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
๐ŸŒ
Google Groups
groups.google.com โ€บ g โ€บ golang-nuts โ€บ c โ€บ 7yGYsdFTkWk
Convert int to int32 or int64
This should do it: myInt := (int)myInt64 Look at the topic conversions in the language spec. Note that this conversion will truncate the value if it doesn't fit into the int. On Sat, Apr 18, 2015 at 9:08 AM Brad Fitzpatrick <brad...@golang.org> wrote:
Discussions

numeric - Go: convert big.Int to regular integer (int, int32, int64) - Stack Overflow
I assumed this would be a simple issue with abundant answers on the Internet. But that does not appear to be the case. I need to convert a Go big.Int into a regular integer (e.g., int, int32, or i... More on stackoverflow.com
๐ŸŒ stackoverflow.com
go - int vs int32 return value - Stack Overflow
I am running into an issue, which seems to be related to int32 vs int data type. My program is returning different values on different environments. For example, on go playground, I notice the va... More on stackoverflow.com
๐ŸŒ stackoverflow.com
How to convert an int64 to int in Go? - Stack Overflow
In Go, what is the best strategy for converting int64 to int? I am having difficulty comparing the two package main import ( "math" "strings" "strconv" ) type largestPrimeFactor str... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Int vs Int8, int 32
When you need an integer value you should use int unless you have a specific reason to use a sized or unsigned integer type. More on reddit.com
๐ŸŒ r/golang
38
26
December 20, 2023
๐ŸŒ
GitHub
github.com โ€บ rung โ€บ go-safecast
GitHub - rung/go-safecast: Go Library for safe type conversion to prevent integer overflow ยท GitHub
The function returns error when the value is out of the 32-bit range. This library also has safecast.Int16 and safecast.Int8. You can use the functions in the same way as safecast.Int32 ยท s := "2147483647" i, err := safecast.Atoi32(s) // convert string to int32 in a safe way if err != nil { return err }
Starred by 18 users
Forked by 3 users
Languages ย  Go 90.2% | Makefile 9.8%
๐ŸŒ
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.
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 77978215 โ€บ go-convert-big-int-to-regular-integer-int-int32-int64
numeric - Go: convert big.Int to regular integer (int, int32, int64) - Stack Overflow
I assumed this would be a simple issue with abundant answers on the Internet. But that does not appear to be the case. I need to convert a Go big.Int into a regular integer (e.g., int, int32, or i...
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
Find elsewhere
๐ŸŒ
Google Groups
groups.google.com โ€บ g โ€บ golang-nuts โ€บ c โ€บ e2UvEGi55mA
golang type casting from interface int32 to int
October 8, 2013 - I'm trying to assign a variable of type interface {} int32 to another variable of type int, and here are the all the errors that I'm getting:
๐ŸŒ
Boyter
boyter.org โ€บ posts โ€บ cost-of-integer-cast-in-go
Cost of a integer cast in Go | Ben E. C. Boyter
August 22, 2022 - So about 0.5 ns for each operation. Which given the clock speed of the CPU means we are observing a single operation. With this as the baseline lets try the int to int32 cast.
๐ŸŒ
Go language Tutorial
golangtutorial.dev โ€บ home โ€บ tips โ€บ how to convert an int32 & int64 to string in go
How to Convert an int32 & int64 to string in Go - Go language Tutorial
November 5, 2020 - strconv.Itoa accepts parameter of type int32 and converts it to string. package main import ( "strconv" "fmt" ) func main() { var intValue int = 100 var stringValue string stringValue = strconv.Itoa(intValue) fmt.Printf("%T, %v\n",intValue,intValue) fmt.Printf("%T, %v\n",stringValue,stringValue) }
๐ŸŒ
Reddit
reddit.com โ€บ r/golang โ€บ converting int32 to uint64 and back to int32
r/golang on Reddit: Converting int32 to uint64 and back to int32
February 5, 2023 -

I am a bit concerned about a piece of code I wrote. It seems to work but I did not quite expect it to...

How does type conversion exactly work in Go when I convert a int32 to uint64 and then back to int32. I was somewhat scared that funny business might happen.

To give practical context of what I am doing. I have data like:

type data struct {
    a uint16
    b uint8
    c uint8
    d int32
}

Instead of using a data struct I want to package all the values in a single uint64 like:

data := uint64(a) | uint64(b)<<16 | uint64(c)<<24 | uint64(d)<<32

and then unpack it like (shifting back and applying a bit mask if necessary):

d := int32(data>>32)

my worries were around:

data = uint64(int32(d))

I was worried that it would try to do some conversion from int32 with negative values. But running some tests all it seems to be doing is padding or truncating bits and interpreting the two's compliment int values as uint.

Is this all safe and correct or am I about to run into nightmarish bugs?

Edit: In essence what I am asking with go type conversions does this hold for every value of d of type int32 :

d == int32(uint64(d))

๐ŸŒ
gosamples
gosamples.dev โ€บ tutorials โ€บ convert int to string in go
๐Ÿ”ข Convert int to string in Go
July 23, 2021 - var number int = 12 // you can use any integer here: int32, int16, int8 str := strconv.FormatInt(int64(number), 10) fmt.Println(str) To convert int to string you can also use strconv.Itoa which is equivalent to strconv.FormatInt(int64(i), 10).
๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ how-to-convert-data-types-in-go
How To Convert Data Types in Go | DigitalOcean
May 9, 2019 - This is done by wrapping the int32() conversion around the index variable. To verify your data types, you could use the fmt.Printf statement and the %T verb with the following syntax: fmt.Printf("index data type: %T\n", index) fmt.Printf("bigIndex data type: %T\n", bigIndex) Outputindex data type: int8 bigIndex data type: int32
๐ŸŒ
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.

๐ŸŒ
Narkive
golang-nuts.narkive.com โ€บ BKuqQw0q โ€บ golang-type-casting-from-interface-int32-to-int
golang type casting from interface int32 to int
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out. ... Post by Tong Sun Hi, I'm trying to assign a variable of type interface {} int32 to another variable - cannot use id (type interface {}) as type int in assignment: need type assertion - panic: interface conversion: interface is int32, not int - cannot use id.(int32) (type int32) as type int in assignment - invalid type assertion: id.(int32).(int) (non-interface type int32 on left)**** what's the proper way to do it?
๐ŸŒ
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?