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)
}
Answer from Shuriken on Stack Overflow
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
🌐
YourBasic
yourbasic.org › golang › int-vs-int64
Pick the right one: int vs. int64 · YourBasic Go
An int64 is the typical choice when memory isn’t an issue. In particular, you can use a byte, which is an alias for uint8, to be extra clear about your intent. Similarly, you can use a rune, which is an alias for int32, to emphasize than an integer represents a code point.
🌐
Reddit
reddit.com › r/golang › is a int the same as int64?
Is a Int the same as Int64? : r/golang
March 24, 2022 - But you can't assign an int to an int64 or vice versa because they're different types. You can explicitly convert one to the other, which may lose info if you're going 64->32. ... I mean, technically, yes, you could compile a program for 32 bits and run that on a 64 bit machine if the OS supports ...
🌐
DEV Community
dev.to › herocod3r › demystifying-the-int-type-in-go-1f63
Demystifying the int type in Go - DEV Community
May 3, 2021 - Go engineers, therefore should be careful when using the int type. If you intend to store a 64bit value, it is better you use an int64 type to avoid any weird outcome.
🌐
Programming.Guide
programming.guide › go › int-vs-int64.html
Go: int vs. int64 | Programming.Guide
In this example the data has type int64, while the index and the length of the slice has type int: func Reverse(a []int64) { n := len(a) for i := 0; i < n/2; i++ { a[i], a[n-i-1] = a[n-i-1], a[i] } } ... // A Duration represents the elapsed time between two instants // as an int64 nanosecond count. The representation limits the // largest representable duration to approximately 290 years. type Duration int64
Find elsewhere
🌐
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?

🌐
Go Tutorial
golangbot.com › types
Basic Data Types in Go | golangbot.com
May 5, 2024 - In the above program a is of type int and the type of b is inferred from the value assigned to it (95). As we have stated above, the size of int is 32 bit in 32 bit systems and 64 bit in 64 bit systems.
🌐
Educative
educative.io › answers › what-is-type-int64-in-golang
What is type int64 in Golang?
int is one of the available numeric data types in Go used to store signed integers. int64 is a version of int that only stores signed numeric values composed of up to 64 bits.
🌐
Medium
medium.com › @LukePetersonAU › understanding-integer-types-in-go-a55453f5ae00
Understanding integer types in Go | by Luke Peterson | Medium
April 8, 2024 - int32 — Represents a 32-bit signed integer. Range: -2,147,483,648 to 2,147,483,647 · int64 — Represents a 64-bit signed integer.
🌐
Boot.dev
blog.boot.dev › golang › default-native-types-golang
Don't Go to Casting Hell - Use Default Native Types in Go | Boot.dev
May 21, 2020 - In all of the above cases, the choice of specific sub-types are based on range and precision. int8 can store values between -128 and 127, while int64 ranges from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
🌐
Quora
quora.com › In-Golang-why-is-the-integer-type-written-as-int-but-the-float-type-as-float64-or-float32
In Golang, why is the integer type written as int, but the float type as float64 or float32? - Quora
Consider the really simple operation of adding 1 to ... Go also supports int32 and int64 (and int8, int16, and unsigned variants of all of these), for when you need to specify the precise size.
🌐
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.

🌐
Rotational
rotational.io › blog › ranges-of-integer-data-types
Rotational Labs | Ranges of Integer Data Types
// Constants defined in the math package const ( MaxInt8 int8 = 1<<7 - 1 MinInt8 int8 = -1 << 7 MaxInt16 int16 = 1<<15 - 1 MinInt16 int16 = -1 << 15 MaxInt32 int32 = 1<<31 - 1 MinInt32 int32 = -1 << 31 MaxInt64 int64 = 1<<63 - 1 MinInt64 int64 = -1 << 63 MaxUint8 uint8 = 1<<8 - 1 MaxUint16 uint16 = 1<<16 - 1 MaxUint32 uint32 = 1<<32 - 1 MaxUint64 uint64 = 1<<64 - 1 ) // Complement method (system specific) const ( MaxUint uint = ^uint(0) MinUint uint = 0 MaxInt int = int(MaxUint >> 1) MinInt int = -MaxInt - 1 )
🌐
Sololearn
sololearn.com › en › Discuss › 2682964 › golang-int8-int16-int32-float32-
Golang int8, int16, int32, float32 .... | Sololearn: Learn to code for FREE!
they all give a similar result: a LIMITED integer. the optional leading 'u' is for unsigned (versus implicitly signed). the number after the 'int' give you the number of bits used to store the integer: 8 bits hold unsigned values from 0 to 2^8 ...
🌐
Reddit
reddit.com › r/golang › int64 vs int32 performance in synthetic test
r/golang on Reddit: Int64 vs Int32 performance in synthetic test
January 14, 2021 -

I asked this question in the official Gophers Slack channel, but didn't really get an answer so I try it here:

So I stumbled on this synthetic test (I know...) the author uses int and runs the tests on a 64bit machine the result is about 7 seconds to finish it, in the comments someone suggested to use explicitly int32 which turns out to be much faster with 2 seconds to complete the task.

My question is why is int64 so much slower?

https://itnext.io/compare-c-js-python-python-numba-php7-php8-and-golang-in-prime-number-calculation-55e82b6f82a9