Coding Rooms is discontinuing Free and K-12 services. Click here for more information.

GoLang Int to String

Convert an int/int64 to a string in GoLang

October 29, 2020
by
Sasha Varlamov

Convert int to string in GoLang

The simplest way to convert an int to string in Go is to use the fmt.Sprint function. This will also work seamlessly for int64 types.

package main

import "fmt"

func main() {
    myInt := 10
    myStr := fmt.Sprint(myInt)
    fmt.Println("My int: ", myInt)
    fmt.Println("My string: ", myStr)
}

Alternatives to convert int to string

Go also ships with a package called strconv that can be used to convert an int (not int64) to a string:

package main

import (
    "fmt"
    "strconv"
)

func main() {
    myInt := 10
    myStr := strconv.Itoa(myInt)
    fmt.Println("My int: ", myInt)
    fmt.Println("My string: ", myStr)
}

Conclusion

Go has a number of easy builtin functions that can be used for converting (and formatting) int data to strings.

Sasha Varlamov
Sasha Varlamov

Coding Rooms
Founder & CEO

October 29, 2020

Learn why these organizations choose Coding Rooms as their next-generation learning platform.