🌐
GitHub
github.com › rung › go-safecast
GitHub - rung/go-safecast: Go Library for safe type conversion to prevent integer overflow · GitHub
i := 2147483647 i32, err := safecast.Int32(i) // convert int 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%
🌐
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:
🌐
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
🌐
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?

Find elsewhere
🌐
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've tried int(bigVal), int32(bigVal), and int64(bigVal), but they give the error cannot convert bigVal(variable of type big.Int) to type x (where x is either int, int32 or int64).
🌐
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) }
🌐
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 ...
🌐
Sololearn
sololearn.com › en › Discuss › 2682964 › golang-int8-int16-int32-float32-
Golang int8, int16, int32, float32 .... | Sololearn: Learn to code for FREE!
because sometimes int32 or below is enough (and it could make a memory footprint very different for large dataset). 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)
🌐
YourBasic
yourbasic.org › golang › convert-int-to-string
Convert between int, int64 and string · YourBasic Go
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.
🌐
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))) }