As per documentation

func ParseInt(s string, base int, bitSize int) (i int64, err error)

ParseInt always return int64 no matter what. Moreover

The bitSize argument specifies the integer type that the result must fit into

So basically the your bitSize parameter only tells that the string value that you are going to parse should fit the bitSize after parsing. If not, out of range will happen.

Like in this PlayGround: strconv.ParseInt("192", 10, 8) (as you see the value after the parsing would be bigger than maximum value of int8).

If you want to parse it to whatever value you need, just use int8(i) afterwards (int8, int16, int32).

P.S. because you touched the topic how to convert to specific intX, I would outline that it is also possible to convert to unsigned int.

Answer from Salvador Dali on Stack Overflow
๐ŸŒ
Go Packages
pkg.go.dev โ€บ strconv
strconv package - strconv - Go Packages
ParseBool, ParseFloat, ParseInt, ... width the result can be converted to that narrower type without data loss: s := "2147483647" // biggest int32 i64, err := strconv.ParseInt(s, 10, 32) ......
Discussions

Convert string to integer type in Go? - Stack Overflow
If you need to parse strings really fast, you can use faiNumber package. faiNumber is the fastest golang string parser library. All of faiNumber's function was benchmark to run way faster than the strconv package. faiNumber supports parsing strings of decimal, binary, octal, hex to an int32, ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
go - Convert int32 to string in Golang - Stack Overflow
I need to convert an int32 to string in Golang. Is it possible to convert int32 to string in Golang without converting to int or int64 first? Itoa needs an int. FormatInt needs an int64. More on stackoverflow.com
๐ŸŒ stackoverflow.com
String to int (not int64) using ParseInt()/Atoi()
Greetings! Question about string to int conversion using package: strconv First, strconv.Atoi() returns type int. Second, it appears that strconv.ParseInt() never returns a type int. However, the language definition says: โ€œThe bitSize argument specifies the integer type that the result must ... More on forum.golangbridge.org
๐ŸŒ forum.golangbridge.org
1
0
May 6, 2020
How to convert an int value to string in Go? - Stack Overflow
If you need to convert an int value to string, you can use faiNumber package. faiNumber is the fastest golang string parser library. All of faiNumber's function was benchmark to run way faster than the strconv package. faiNumber supports converting an int32, uint32, int64, or uint64 value to ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
YourBasic
yourbasic.org โ€บ golang โ€บ convert-int-to-string
Convert between int, int64 and string ยท YourBasic Go
If the first argument is 0, the base is implied by the stringโ€™s prefix: base 16 for "0x", base 8 for "0", and base 10 otherwise. The second argument specifies the integer type that the result must fit into. Bit sizes 0, 8, 16, 32, and 64 correspond to int, int8, int16, int32, and int64.
๐ŸŒ
Scalent -
scalent.io โ€บ home โ€บ golang topics โ€บ string to int conversion in golang
String to Integer Conversion in Golang | Scalent
December 19, 2024 - The bitSize is set to 32, meaning it will check if the parsed value fits within the range of an int32 (-2147483648 to 2147483647).
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
}
Top answer
1 of 5
119

One line answer is fmt.Sprint(i).

Anyway there are many conversions, even inside standard library function like fmt.Sprint(i), so you have some options (try The Go Playground):


1- You may write your conversion function (Fastest):

func String(n int32) string {
    buf := [11]byte{}
    pos := len(buf)
    i := int64(n)
    signed := i < 0
    if signed {
        i = -i
    }
    for {
        pos--
        buf[pos], i = '0'+byte(i%10), i/10
        if i == 0 {
            if signed {
                pos--
                buf[pos] = '-'
            }
            return string(buf[pos:])
        }
    }
}

2- You may use fmt.Sprint(i) (Slow)
See inside:

// Sprint formats using the default formats for its operands and returns the resulting string.
// Spaces are added between operands when neither is a string.
func Sprint(a ...interface{}) string {
    p := newPrinter()
    p.doPrint(a)
    s := string(p.buf)
    p.free()
    return s
}

3- You may use strconv.Itoa(int(i)) (Fast)
See inside:

// Itoa is shorthand for FormatInt(int64(i), 10).
func Itoa(i int) string {
    return FormatInt(int64(i), 10)
}

4- You may use strconv.FormatInt(int64(i), 10) (Faster)
See inside:

// FormatInt returns the string representation of i in the given base,
// for 2 <= base <= 36. The result uses the lower-case letters 'a' to 'z'
// for digit values >= 10.
func FormatInt(i int64, base int) string {
    _, s := formatBits(nil, uint64(i), base, i < 0, false)
    return s
}

Comparison & Benchmark (with 50000000 iterations):

s = String(i)                       takes:  5.5923198s
s = String2(i)                      takes:  5.5923199s
s = strconv.FormatInt(int64(i), 10) takes:  5.9133382s
s = strconv.Itoa(int(i))            takes:  5.9763418s
s = fmt.Sprint(i)                   takes: 13.5697761s

Code:

package main

import (
    "fmt"
    //"strconv"
    "time"
)

func main() {
    var s string
    i := int32(-2147483648)
    t := time.Now()
    for j := 0; j < 50000000; j++ {
        s = String(i) //5.5923198s
        //s = String2(i) //5.5923199s
        //s = strconv.FormatInt(int64(i), 10) // 5.9133382s
        //s = strconv.Itoa(int(i)) //5.9763418s
        //s = fmt.Sprint(i) // 13.5697761s
    }
    fmt.Println(time.Since(t))
    fmt.Println(s)
}

func String(n int32) string {
    buf := [11]byte{}
    pos := len(buf)
    i := int64(n)
    signed := i < 0
    if signed {
        i = -i
    }
    for {
        pos--
        buf[pos], i = '0'+byte(i%10), i/10
        if i == 0 {
            if signed {
                pos--
                buf[pos] = '-'
            }
            return string(buf[pos:])
        }
    }
}

func String2(n int32) string {
    buf := [11]byte{}
    pos := len(buf)
    i, q := int64(n), int64(0)
    signed := i < 0
    if signed {
        i = -i
    }
    for {
        pos--
        q = i / 10
        buf[pos], i = '0'+byte(i-10*q), q
        if i == 0 {
            if signed {
                pos--
                buf[pos] = '-'
            }
            return string(buf[pos:])
        }
    }
}
2 of 5
14

The Sprint function converts a given value to string.

package main

import (
     "fmt"
)

func main() {

      var sampleInt int32 = 1

      sampleString := fmt.Sprint(sampleInt)
      fmt.Printf("%+V %+V\n", sampleInt, sampleString)
}

// %!V(int32=+1) %!V(string=1)

See this example.

๐ŸŒ
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 - The simplest way to convert a string to an integer is to use the Atoi function from the strconv package.
Find elsewhere
๐ŸŒ
Kelche
kelche.co โ€บ blog โ€บ go โ€บ golang-parseint
Golang ParseInt - Kelche
January 17, 2023 - ParseInt can be used to convert a string to an int32.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ go language โ€บ how-to-convert-string-to-integer-type-in-golang
How to Convert string to integer type in Golang? - GeeksforGeeks
July 12, 2025 - 2. ParseInt() Function: The ParseInt interprets a string s in the given base (0, 2 to 36) and bit size (0 to 64) and returns the corresponding value i.
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ how-to-convert-a-string-to-int-in-golang
How to convert a string to int in Golang
A string can be converted into an int data type using the Atoi function provided by the package strconv.
๐ŸŒ
ByteSizeGo
bytesizego.com โ€บ blog โ€บ convert-int-to-string-golang
How to convert an int to string in Golang
September 10, 2024 - For more control over the conversion, particularly with different integer types (such as int64), you can use strconv.FormatInt(). This function converts an integer to a string in any base from 2 to 36.
๐ŸŒ
Bacancy Technology
bacancytechnology.com โ€บ qanda โ€บ golang โ€บ string-to-integer-type-in-go
Exploring Conversion of String to Integer Type in Go
January 3, 2024 - The strconv package in Go provides the Atoi function, allowing you to convert a string to an integer (int) type.
๐ŸŒ
gosamples
gosamples.dev โ€บ tutorials โ€บ convert int to string in go
๐Ÿ”ข Convert int to string in Go
July 23, 2021 - Use strconv.FormatInt function to convert an integer variable to a string in Go. var number int64 = 12 str := strconv.FormatInt(number, 10) fmt.Println(str) var number int = 12 // you can use any integer here: int32, int16, int8 str := strconv.FormatInt(int64(number), 10) fmt.Println(str)
๐ŸŒ
Golang.cafe
golang.cafe โ€บ blog โ€บ golang-string-to-int-conversion-example
Golang String To Int Conversion Example | Golang.cafe
April 27, 2022 - ParseInt interprets a string s ... by the stringโ€™s prefix: 2 for โ€œ0bโ€, 8 for โ€œ0โ€ or โ€œ0oโ€, 16 for โ€œ0xโ€, and 10 otherwise. Also, for argument base 0 only, underscore characters are permitted as defined by the Go syntax for integer literals. The bitSize argument specifies the integer type that the result must fit into. Bit sizes 0, 8, 16, 32, and 64 correspond to int, int8, int16, int32, and ...
๐ŸŒ
Metaschool
metaschool.so โ€บ home โ€บ answers โ€บ how to convert string to integer in go? golang string to int guide
How to Convert String to Integer in Go? Golang String to Int Guide
January 16, 2025 - Learn how to convert a string to integer in Go using methods like strconv.Atoi, strconv.ParseInt, and custom implementations. Explore examples and use cases.
๐ŸŒ
Medium
medium.com โ€บ @kode-n-rolla โ€บ type-conversion-in-go-a-handy-cheat-sheet-for-developers-51482d4dca0c
Type Conversion in Go: A Handy Cheat Sheet for Developers | by k0de-n-ะฏolla | Medium
February 27, 2026 - Explicitly cast the rune to int32 and subtract the ASCII value of '0': func main() { str := "12345" for _, r := range str { digit := int(r) - int('0') // Convert rune to int and subtract '0' fmt.Printf("Rune: %c, Digit: %d\n", r, digit) } // Output: // Rune: 1, Digit: 1 // Rune: 2, Digit: 2 // Rune: 3, Digit: 3 // Rune: 4, Digit: 4 // Rune: 5, Digit: 5 } ... Goโ€™s strings are UTF-8 encoded, so they can include multibyte characters like emojis or non-Latin scripts.