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

Is a Int the same as Int64?
What does the Go documentation say? More on reddit.com
🌐 r/golang
15
0
March 24, 2022
What’s the difference between int64 and C.int64_t?
C. Is to interact with C code from go. So you can link with a C library and call it from go. The C library takes types in C, so you need to use C.int for an int in C from go. And so on. It must use some C library and, therefore, need to use the C type on some parts. For the others, it just uses the go native type. But this is an educated guess, quite hard to know the reason without understanding the app. It's the simplest explanation I can think of given the info you shared :) More on reddit.com
🌐 r/golang
12
4
October 19, 2022
How to compare int to int64?
My idea was to compare int and int64 by casting int to int64. I expected the comparison to recognize both maps as equal. How can I achieve the desired behavior? More on github.com
🌐 github.com
4
January 5, 2022
Pick the right one: int vs. int64

One of thing is don't try to optimize and be too smart about int size, it will bit you on the end, if you're not sure just use an int64.

More on reddit.com
🌐 r/golang
4
17
August 14, 2020
🌐
Reddit
reddit.com › r/golang › is a int the same as int64?
Is a Int the same as Int64? : r/golang
March 24, 2022 - An int is 32 bits on a 32 bit machine and 64 bits on a 64 bit machines. An int64 is obviously always 64 bits. But you can't assign an int to an int64 or vice versa because they're different types.
🌐
Educative
educative.io › answers › what-is-type-int64-in-golang
What is type int64 in Golang?
To restrict the type of data stored ... 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....
🌐
Google Groups
groups.google.com › g › golang-nuts › c › 3rH1e4wnFR8
int or int64
I'd say just use int for array indices and such. It should always be enough, since in 32 bit systems you can't address more than 4GB of bytes, which implies that you'll have arrays and lists shorter than 4GB, which means that normal int is sufficient.
🌐
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.
Find elsewhere
🌐
Programming.Guide
programming.guide › go › int-vs-int64.html
Go: int vs. int64 | Programming.Guide
When memory isn't an issue, int64 is often the natural choice. In this example the data has type int64, while the index and the length of the slice has type int:
🌐
GitHub
github.com › google › go-cmp › issues › 286
How to compare int to int64? · Issue #286 · google/go-cmp
January 5, 2022 - package main import ( "fmt" "github.com/google/go-cmp/cmp" ) func main() { fmt.Print(cmp.Diff( map[string]interface{}{"a": 1, "b": 2, "c": int64(3)}, map[string]interface{}{"a": int64(1), "b": 2, "c": int64(3)}, cmp.Transformer("intToInt64", func(i int) int64 { return int64(i) }), )) }
Author   pofl
🌐
Reddit
reddit.com › r/golang › pick the right one: int vs. int64
r/golang on Reddit: Pick the right one: int vs. int64
August 14, 2020 - Use the size you need. If you need 64 bit integers, use int64. If you don't care, use int.
🌐
Navneet Pandey
navneetpandey.com › int-vs-int64
int vs int64 - Navneet Pandey
March 13, 2023 - Mar 13, 2023 golang · When we use int the system arch dictates if it will be int32 or int64. This is because go compiler will choose the most efficient integer size. So when using the int32 or int64 make sure you have a need for it. Meanwhile, floats are based on IEEE754 specification.
Top answer
1 of 3
27

int and uint are only 64-bit on 64-bit architectures. On 32-bit architectures they are 32 bits.

The general answer is that, unless you need a certain precision, sticking with datatypes that are the same size as a word on the current architecture (e.g. 32 bits on a 32-bit architecture) is typically slightly more efficient.

2 of 3
8

int and uint correspond to the maximum possible length of basic Go data structures in a Go implementation and runtime. The length of string, map[K]T, []T and chan T always fits into an int, and the capacity of []T and chan T always fits into an int.

An allocation via make is bound to return an object with length and capacity that always fit into an int. The built-in function append returns a slice with length and capacity that never exceed an int. The length (the number of defined keys) of a map after inserting a new key-value pair always fits into an int.

The main benefit is that int and uint are the smallest (in terms of bitsize) data types which are safe to use in a Go program in conjunction with common Go data types such as slices and maps.

The size of int is independent from the size of a pointer *T. The integer type corresponding to *T is uintptr. In theory, a Go implementation could choose to map int to int16 - many Go programs would remain to work correctly, but limiting the size of allocations to 15 bits may be too restrictive and would cause runtime panics.

On 64-bit architectures Go 1.0 has int and uint 32 bits long, Go 1.1 64 bits long (see Go 1.1 Release Notes). This change will increase the memory usage of some Go programs on 64-bit architectures..

Explicitly using int64 instead of int in a Go program can make it slower under Go 1.0 and on 32-bit architectures because:

  • of conversions between int and int64

  • the performance of certain CPU instructions, such as division, depends on the size of operands

Explicitly using int64 instead of int in a Go program can make it faster under Go 1.0 on 64-bit architectures because:

  • The compiler can generate more efficient code for address and offset computation if all operands are 64-bit. Mixing 32-bit and 64-bit operands when computing addresses and offsets is slower.

Using uint16 as an index when accessing [1<<16]T allows the compiler to remove bound checking instructions.

🌐
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.
🌐
YourBasic
yourbasic.org › golang › convert-int-to-string
Convert between int, int64 and string · YourBasic Go
Warning: In a plain conversion the value is interpreted as a Unicode code point, and the resulting string will contain the character represented by that code point, encoded in UTF-8. ... Use strconv.FormatInt to format an int64 in a given base.
🌐
GitHub
github.com › aerospike › aerospike-client-go › issues › 5
int64 vs int · Issue #5 · aerospike/aerospike-client-go
August 22, 2014 - Hi, when I store a value as 'int64' type, it is ok. But when I read the value back, the type is 'int'. Is it a bug? ... You can’t perform that action at this time.
Author   csimplestring