The textbook you are looking for is

The Go Programming Language Specification

Numeric types

A numeric type represents sets of integer or floating-point values. The predeclared architecture-independent numeric types are:

uint32 set of all unsigned 32-bit integers (0 to 4294967295)
uint64 set of all unsigned 64-bit integers (0 to 18446744073709551615)

int32  set of all signed 32-bit integers (-2147483648 to 2147483647)
int64  set of all signed 64-bit integers (-9223372036854775808 to 9223372036854775807)

There is also a set of predeclared numeric types with implementation-specific sizes:

uint either 32 or 64 bits
int  same size as uint

Check the size of type int. On the Go Playground it's 4 bytes or 32 bits.

package main

import (
    "fmt"
    "runtime"
    "unsafe"
)

func main() {
    fmt.Println("arch", runtime.GOARCH)
    fmt.Println("int", unsafe.Sizeof(int(0)))
}

Playground: https://play.golang.org/p/2A6ODvhb1Dx

Output (Playground):

arch amd64p32
int 4

Run the program in your (LeetCode) environment. It's likely 8 bytes or 64 bits.

For example, in my environment,

Output (Local):

arch amd64
int 8

Here are some fixes to your code,

package main

import (
    "fmt"
    "runtime"
)

func main() {
    fmt.Println(runtime.GOARCH)
    fmt.Printf("%v\n", singleNumber([]int{-2, -2, 1, 1, -3, 1, -3, -3, -4, -2}))
}

func singleNumber(nums []int) int {
    sum := make([]int, 64)

    for _, v := range nums {
        for i := range sum {
            sum[i] += 1 & (v >> uint(i))
        }
    }

    res := 0
    for k, v := range sum {
        if (v % 3) != 0 {
            res |= (v % 3) << uint(k)
        }
    }
    fmt.Printf("res %+v\n", res)
    return res
}

Playground: https://play.golang.org/p/kaoSuesu2Oj

Output (Playground):

amd64p32
res -4
-4

Output (Local):

amd64
res -4
-4
Answer from peterSO on Stack Overflow
🌐
Educative
educative.io › answers › what-is-type-int32-in-golang
What is type int32 in Golang?
int is one of the available numeric data types in Go used to store signed integers. int32 is a version of int that only stores signed numeric values composed of up to 32 bits.
Top answer
1 of 1
24

The textbook you are looking for is

The Go Programming Language Specification

Numeric types

A numeric type represents sets of integer or floating-point values. The predeclared architecture-independent numeric types are:

uint32 set of all unsigned 32-bit integers (0 to 4294967295)
uint64 set of all unsigned 64-bit integers (0 to 18446744073709551615)

int32  set of all signed 32-bit integers (-2147483648 to 2147483647)
int64  set of all signed 64-bit integers (-9223372036854775808 to 9223372036854775807)

There is also a set of predeclared numeric types with implementation-specific sizes:

uint either 32 or 64 bits
int  same size as uint

Check the size of type int. On the Go Playground it's 4 bytes or 32 bits.

package main

import (
    "fmt"
    "runtime"
    "unsafe"
)

func main() {
    fmt.Println("arch", runtime.GOARCH)
    fmt.Println("int", unsafe.Sizeof(int(0)))
}

Playground: https://play.golang.org/p/2A6ODvhb1Dx

Output (Playground):

arch amd64p32
int 4

Run the program in your (LeetCode) environment. It's likely 8 bytes or 64 bits.

For example, in my environment,

Output (Local):

arch amd64
int 8

Here are some fixes to your code,

package main

import (
    "fmt"
    "runtime"
)

func main() {
    fmt.Println(runtime.GOARCH)
    fmt.Printf("%v\n", singleNumber([]int{-2, -2, 1, 1, -3, 1, -3, -3, -4, -2}))
}

func singleNumber(nums []int) int {
    sum := make([]int, 64)

    for _, v := range nums {
        for i := range sum {
            sum[i] += 1 & (v >> uint(i))
        }
    }

    res := 0
    for k, v := range sum {
        if (v % 3) != 0 {
            res |= (v % 3) << uint(k)
        }
    }
    fmt.Printf("res %+v\n", res)
    return res
}

Playground: https://play.golang.org/p/kaoSuesu2Oj

Output (Playground):

amd64p32
res -4
-4

Output (Local):

amd64
res -4
-4
🌐
Go Packages
pkg.go.dev › builtin
builtin package - builtin - Go Packages
int is a signed integer type that is at least 32 bits in size. It is a distinct type, however, and not an alias for, say, int32.
🌐
Reddit
reddit.com › r/golang › using int32 for efficiency or simply stick to int64?
r/golang on Reddit: Using int32 for efficiency or simply stick to int64?
January 30, 2022 -

I have a field in my data which is small enough to simply use an int32. However, from what I can tell, int32 is generally used when representing a code point or something quite specific, rather than using it simply to save 32 bits of storage. Also, some of the stdlib packages (like strconv) deal exclusively with int64 rather than int32. This has meant changes to my code I didn't expect purely to handle int32 things.

What is your opinion (and perhaps the general consensus) on using int32 purely for efficiency purposes? Is it just worth using an int64 for simplicity?

🌐
YourBasic
yourbasic.org › golang › int-vs-int64
Pick the right one: int vs. int64 · YourBasic Go
The int type is either 32 or 64 bits, and always big enough to hold the maximum possible length of an array. See Maximum value of an int for code to compute the maximum value of an int.
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.

Find elsewhere
🌐
DEV Community
dev.to › herocod3r › demystifying-the-int-type-in-go-1f63
Demystifying the int type in Go - DEV Community
May 3, 2021 - func printAll(){ fmt.Printf(" int %d,\n int8 %d, \n int16 %d,\n int32 %d, \n int64 %d", unsafe.Sizeof(int(0)), unsafe.Sizeof(int8(0)), unsafe.Sizeof(int16(0)), unsafe.Sizeof(int32(0)), unsafe.Sizeof(int64(0))) } The result is · Wow, coming from a C# background where an int is always 4 byte, seeing 8bytes for an int is mind boggling.
Top answer
1 of 6
78
func ParseInt(s string, base int, bitSize int) (i int64, err error)

ParseInt always returns int64.

bitSize defines the range of values.

If the value corresponding to s cannot be represented by a signed integer of the given size, err.Err = ErrRange.

http://golang.org/pkg/strconv/#ParseInt

type int int

int is a signed integer type that is at least 32 bits in size. It is a distinct type, however, and not an alias for, say, int32.

http://golang.org/pkg/builtin/#int

So int could be bigger than 32 bit in the future or on some systems like int in C.

I guess on some systems int64 might be faster than int32 because that system only works with 64-bit integers.

Here is an example of an error when bitSize is 8:

http://play.golang.org/p/_osjMqL6Nj

package main

import (
    "fmt"
    "strconv"
)

func main() {
    i, err := strconv.ParseInt("123456", 10, 8)
    fmt.Println(i, err)
}
2 of 6
36

Package strconv

func ParseInt

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

ParseInt interprets a string s in the given base (2 to 36) and returns the corresponding value i. If base == 0, the base is implied by the string's prefix: base 16 for "0x", base 8 for "0", and base 10 otherwise.

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 int64.

The errors that ParseInt returns have concrete type *NumError and include err.Num = s. If s is empty or contains invalid digits, err.Err = ErrSyntax; if the value corresponding to s cannot be represented by a signed integer of the given size, err.Err = ErrRange.

ParseInt always returns an int64 value. Depending on bitSize, this value will fit into int, int8, int16, int32, or int64. If the value cannot be represented by a signed integer of the size given by bitSize, then err.Err = ErrRange.

The Go Programming Language Specification

Numeric types

The value of an n-bit integer is n bits wide and represented using two's complement arithmetic.

int8        the set of all signed  8-bit integers (-128 to 127)
int16       the set of all signed 16-bit integers (-32768 to 32767)
int32       the set of all signed 32-bit integers (-2147483648 to 2147483647)
int64       the set of all signed 64-bit integers (-9223372036854775808 to 9223372036854775807)

There is also a set of predeclared numeric types with implementation-specific sizes:

uint     either 32 or 64 bits
int      same size as uint

int is either 32 or 64 bits, depending on the implementation. Usually it's 32 bits for 32-bit compilers and 64 bits for 64-bit compilers.

To find out the size of an int or uint, use strconv.IntSize.

Package strconv

Constants

const IntSize = intSize

IntSize is the size in bits of an int or uint value.

For example,

package main

import (
    "fmt"
    "runtime"
    "strconv"
)

func main() {
    fmt.Println(runtime.Compiler, runtime.GOARCH, runtime.GOOS)
    fmt.Println(strconv.IntSize)
}

Output:

gc amd64 linux
64
🌐
ZetCode
zetcode.com › golang › builtins-int32-type
Understanding the int32 Type in Golang
May 8, 2025 - The int32 type represents signed 32-bit integers in Go. It can store values from -2,147,483,648 to 2,147,483,647.
🌐
Reddit
reddit.com › r/golang › behavior of converting uint32 --> int on 32-bit vs 64-bit systems
r/golang on Reddit: Behavior of converting uint32 --> int on 32-bit vs 64-bit systems
January 3, 2024 -

From what I understand, the size of the int type in Go is platform dependent.

From the docs (tour):

The int, uint, and uintptr types are usually 32 bits wide on 32-bit systems and 64 bits wide on 64-bit systems. When you need an integer value you should use int unless you have a specific reason to use a sized or unsigned integer type.

So, on a 32-bit system, if I did something like this:

var myUint32 uint32 = 4294967295 // max positive value for uint32

myInt := int(myUint32)

what would happen to myInt on a 32-bit system? I can't seem to find docs for this. Would the bitwise value remain the same in memory (and using a bit as a sign bit)? Or would there be truncation of some sort? I am unsure. I also don't have a quick way to test this out.

backstory, I have code in review that was suggested to be written like this:

func myFunc (a, b uint32) int {
    return int(a) - int(b)
}

I have a really bad feeling about this, especially since our code planned to be open source. There is no guarantee what architecture this will run on. But I need solid evidence argue we shouldn't do this.

🌐
Sololearn
sololearn.com › en › Discuss › 2682964 › golang-int8-int16-int32-float32-
Golang int8, int16, int32, float32 .... | Sololearn: Learn to code for FREE!
however, basic use for few variable are commonly done with the widest int type available for your cpu (usualy int64 or int32 depending on platform)
🌐
GitHub
github.com › golang › go › issues › 26545
Proposal: flag: add support for int32 and uint32 options · Issue #26545 · golang/go
July 23, 2018 - Currently many core libraries like grpc have configurable flags and options that are either int32 or uint32, e.g. HTTP2 Server Max Concurrent Streams Internal Poll Socket Opt in Windows This forces the userland code for any configurable ...
Author   rvegas
🌐
gosamples
gosamples.dev › tutorials › convert int to string in go
🔢 Convert int to string in Go
July 23, 2021 - var number int64 = 12 // int, int32, int16, int8 str := strconv.FormatInt(number, 2) fmt.Println(str)