For example strconv.Atoi.

Code:

package main

import (
    "fmt"
    "strconv"
)

func main() {
    s := "123"

    // string to int
    i, err := strconv.Atoi(s)
    if err != nil {
        // ... handle error
        panic(err)
    }

    fmt.Println(s, i)
}
Answer from peterSO on Stack Overflow
๐ŸŒ
Microsoft Learn
learn.microsoft.com โ€บ en-us โ€บ dotnet โ€บ csharp โ€บ programming-guide โ€บ types โ€บ how-to-convert-a-string-to-a-number
How to convert a string to a number - C# | Microsoft Learn
Download Microsoft Edge More info about Internet Explorer and Microsoft Edge ... Access to this page requires authorization. You can try signing in or changing directories. Access to this page requires authorization. You can try changing directories. ... You convert a string to a number by calling the Parse or TryParse method found on numeric types (int, long, double, and so on), or by using methods in the System.Convert class.
Top answer
1 of 6
661

For example strconv.Atoi.

Code:

package main

import (
    "fmt"
    "strconv"
)

func main() {
    s := "123"

    // string to int
    i, err := strconv.Atoi(s)
    if err != nil {
        // ... handle error
        panic(err)
    }

    fmt.Println(s, i)
}
2 of 6
160

Converting Simple strings

The easiest way is to use the strconv.Atoi() function.

Note that there are many other ways. For example fmt.Sscan() and strconv.ParseInt() which give greater flexibility as you can specify the base and bitsize for example. Also as noted in the documentation of strconv.Atoi():

Atoi is equivalent to ParseInt(s, 10, 0), converted to type int.

Here's an example using the mentioned functions (try it on the Go Playground):

flag.Parse()
s := flag.Arg(0)

if i, err := strconv.Atoi(s); err == nil {
    fmt.Printf("i=%d, type: %T\n", i, i)
}

if i, err := strconv.ParseInt(s, 10, 64); err == nil {
    fmt.Printf("i=%d, type: %T\n", i, i)
}

var i int
if _, err := fmt.Sscan(s, &i); err == nil {
    fmt.Printf("i=%d, type: %T\n", i, i)
}

Output (if called with argument "123"):

i=123, type: int
i=123, type: int64
i=123, type: int

Parsing Custom strings

There is also a handy fmt.Sscanf() which gives even greater flexibility as with the format string you can specify the number format (like width, base etc.) along with additional extra characters in the input string.

This is great for parsing custom strings holding a number. For example if your input is provided in a form of "id:00123" where you have a prefix "id:" and the number is fixed 5 digits, padded with zeros if shorter, this is very easily parsable like this:

s := "id:00123"

var i int
if _, err := fmt.Sscanf(s, "id:%5d", &i); err == nil {
    fmt.Println(i) // Outputs 123
}
Discussions

Which is the best way to convert int to string in golang ?
  1. is plain wrong

  2. is the most lightweight (in terms of dependency and performance)

  3. is the most flexible

More on reddit.com
๐ŸŒ r/golang
7
6
September 16, 2018
How to convert an int value to string in Go? - Stack Overflow
I'm an experienced programmer writing my first Golang code right now and am in that situation. 2019-02-20T00:15:21.75Z+00:00 ... Thank you very much. This is the a convenient and easy to remember solution (my point of view). 2023-01-27T09:55:58.04Z+00:00 ... // ToString Change arg to string func ToString(arg interface... More on stackoverflow.com
๐ŸŒ stackoverflow.com
go - Golang: How to convert string to []int? - Stack Overflow
I tackle with this question. I need to convert strings to int. In this case, I need to convert "5 2 4 6 1 3" to, for example, [6]int{5,2,4,6,1,3}. I wrote following this code, especially AizuArray... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Which is the best way to convert int to string in golang ? : golang
๐ŸŒ r/golang
๐ŸŒ
Sentry
sentry.io โ€บ sentry answers โ€บ go โ€บ converting a string to an integer in go
Converting a string to an integer in Go | Sentry
December 15, 2024 - Note: Although the above example uses base 16 and bit size 32, ParseInt still returns a 64-bit integer. If you want to convert a string to another numerical type, you can use the ParseFloat, ParseUint, or ParseComplex functions, also from the strconv package.
๐ŸŒ
Golang Cafe
golang.cafe โ€บ blog โ€บ golang-string-to-int-conversion-example
Golang String To Int Conversion Example | Golang.cafe
April 26, 2022 - One more sophisticated option is to use the fmt.Sscanf function. Which allows you to parse integer values inside strings but using different format options
๐ŸŒ
Golang.cafe
golang.cafe โ€บ blog โ€บ golang-int-to-string-conversion-example
Golang Int To String Conversion Example | Golang.cafe
April 27, 2022 - Go (Golang) provides string and integer conversion directly from a package coming from the standard library - strconv. In order to convert an integer value to string in Golang we can use the FormatInt function from the strconv package.
Find elsewhere
๐ŸŒ
Ahmad Rosid
ahmadrosid.com โ€บ blog โ€บ golang-int-to-string
How to convert int to string in Go?
October 6, 2021 - res, err := strconv.ParseInt("10001", 10, 64) if err != nil { ... } int8(str)
๐ŸŒ
Go Packages
pkg.go.dev โ€บ strconv
strconv package - strconv - Go Packages
FormatComplex converts the complex number c to a string of the form (a+bi) where a and b are the real and imaginary parts, formatted according to the format fmt and precision prec. The format fmt and precision prec have the same meaning as in FormatFloat. It rounds the result assuming that the original was obtained from a complex value of bitSize bits, which must be 64 for complex64 and 128 for complex128. func FormatFloat(f float64, fmt byte, prec, bitSize int) string
๐ŸŒ
Go
go.dev โ€บ doc โ€บ effective_go
Effective Go - The Go Programming Language
Interfaces in Go provide a way to specify the behavior of an object: if something can do this, then it can be used here. We've seen a couple of simple examples already; custom printers can be implemented by a String method while Fprintf can generate output to anything with a Write method.
๐ŸŒ
Google Groups
groups.google.com โ€บ g โ€บ golang-nuts โ€บ c โ€บ v4eJ5HK3stI
How to do conversion from IP integer to string in golang
May 8, 2012 - to Kyle Lemons, golan...@googlegroups.com, MichaelXu, r...@golang.org ยท I created a solution that works for me. Two functions that do the conversion between net.IP and int64 (I'm using int64 as mongodb does not support uint).
๐ŸŒ
Golangcookbook
golangcookbook.com โ€บ chapters โ€บ numbers โ€บ string_to_num
Converting a string to number
The strconv package also contains functions such as ParseUint and ParseFloat which convert a string to unsigned integer and floating point numbers respectively.
๐ŸŒ
Medium
blog.poloxue.com โ€บ exploring-high-efficiency-int-to-string-conversion-strategies-and-source-code-in-go-090c4fa47789
Exploring High-Efficiency int to string Conversion Strategies and Source Code in Go | by poloxue | Medium
January 20, 2024 - The most direct and commonly used method is the Itoa function from the strconv package. Itoa is an abbreviation for "Integer to ASCII". It provides a quick and concise way to convert integers to strings.
๐ŸŒ
GoLinuxCloud
golinuxcloud.com โ€บ home โ€บ golang convert int to string [100% working]
Golang convert INT to STRING [100% Working] | GoLinuxCloud
November 10, 2023 - In this example, we convert the integer 42 to the string "42" using strconv.Itoa and print the result ... strconv.FormatInt is a function provided by the strconv package in Golang to convert an integer to a string while allowing the flexibility ...
๐ŸŒ
Bacancy Technology
bacancytechnology.com โ€บ qanda โ€บ golang โ€บ convert-string-to-int64
How to Convert Strings to int64 in Go: A Complete Guide
June 4, 2024 - Note that strconv.Atoi returns an int, not an int64. If you need an int64, you should stick with strconv.ParseInt. Reading from Environment Variables Environment variables are often strings, even when they represent numerical values.
๐ŸŒ
Go
go.dev โ€บ ref โ€บ spec
The Go Programming Language Specification - The Go Programming Language
January 12, 2026 - To avoid portability issues all numeric types are defined types and thus distinct except byte, which is an alias for uint8, and rune, which is an alias for int32. Explicit conversions are required when different numeric types are mixed in an expression or assignment. For instance, int32 and int are not the same type even though they may have the same size on a particular architecture. A string type represents the set of string values.