Every language has it’s ways of converting data types. One of the most common data types to convert between is that of strings (
string) and integers (
If you’ve ever tried to do this in Golang, you would know that’s it isn’t all that simple at first.
The easiest way to convert between String and Integers in Golang is as follows:
String to Integer
My go-to option to resolve this problem would be with the
Integer to String
Just as we used the
strconv before, we can also do this in reverse as follows:
Other ways to do this
There are actually 3 ways to do this, even though we used the
strconv.Itoa method above.
strconv.Atoi(...)is very fast and implemented like this:
strconv.ParseInt(...) is also very fast and implemented like this:
fmt.Sscanf(...) is a bit slower but most flexible generally and implemented like this: