Convert string to int & vice-versa in Go

📅️ Published: May 31, 2020  • 🕣 1 min read

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

Tired of fucking up with git everyday?

ugit helps you undo git commands with ease. Undo from 20+ git scenarios

ugit cli demo screen