GoLang : String to Int & Int64 Conversion and Int & int64 to String Conversion
Conversion of datatype is a normal task of every developer. So, here I would be giving you a snippet of how to convert string to int and int64 and vice versa.
String to Int Conversion:
String to Int conversation => strconv.Atoi function return integer with error if any.
Atoi means “A” String to “i” integer
str := "12345"intI, err := strconv.Atoi(str)
Full Script :
Golang Playground : https://play.golang.org/p/obJKzJJXSpP
String to Int64 Conversion:
String to Int64 conversation => strconv.ParseInt function return int64 with error if any.
strconv.ParseInt() => function convert string to int64
str := "12345"int64I, err := strconv.ParseInt(str, 10, 64)
Full Script :
Golang Playground : https://play.golang.org/p/DQGJga3Rb8m
Int & Int64 to String Conversion:
To convert Int to String we have to use strconv.Itoa(int) function.
To convert Int64 to String we have to use strconv.FormatInt(int64, 10) function.
var i int = 12345
var i64 int64 = 12345// Conversion of Int to String
strInt := strconv.Itoa(i)
// Conversion of Int64 to String
strInt64 := strconv.FormatInt(i64, 10)
Full Script :
Golang Playground : https://play.golang.org/p/k1AHCHGVi7w