Today I Learned
Search…
Convert `string` to `int` and vice-versa in Go
There are basically 2-3 methods to convert integer to string and back to integer but the most easiest way is to use the Itoa and Atoi methods in the strconv package.

Demo

1
package main
2
​
3
import (
4
"fmt"
5
"strconv"
6
)
7
​
8
func main() {
9
// String to Int
10
nice, _ := strconv.Atoi("69")
11
fmt.Printf("%T", nice)
12
​
13
//Integer to String
14
high := strconv.Itoa(420)
15
fmt.Printf("\n%T", high)
16
}
Copied!
Atoi returns a second value (err ignored in this example for easier understanding)
The above code should output:
1
int
2
string
Copied!
See online demo
Copy link
Edit on GitHub
Contents
Demo