Convert string to int & vice-versa in Go

📅 May 31, 2020 • 🕣 1 min read • 156 totalwords

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

package main

import (
    "fmt"
    "strconv"
)

func main() {
	// String to Int
	nice, _ := strconv.Atoi("69")
	fmt.Printf("%T", nice)

	//Integer to String
	high := strconv.Itoa(420)
	fmt.Printf("\n%T", high)
}

Atoi returns a second value (err ignored in this example for easier understanding)

The above code should output:

int
string

See online demo