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

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
int64 vs int
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? More on github.com
🌐 github.com
2
August 22, 2014
String to int (not int64) using ParseInt()/Atoi()
Greetings! Question about string to int conversion using package: strconv First, strconv.Atoi() returns type int. Second, it appears that strconv.ParseInt() never returns a type int. However, the language definition says: “The bitSize argument specifies the integer type that the result must ... More on forum.golangbridge.org
🌐 forum.golangbridge.org
1
0
May 6, 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.
🌐
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.
🌐
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.
🌐
Programming.Guide
programming.guide › go › int-vs-int64.html
Go: int vs. int64 | Programming.Guide
// 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 · What's the maximum value of an int?
🌐
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
🌐
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.
🌐
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.
🌐
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 - Members · Online • · korthaj ... by user · Reply · reply · [deleted] • · Use the size you need. If you need 64 bit integers, use int64....
🌐
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?
Author   csimplestring
🌐
GoLinuxCloud
golinuxcloud.com › home › programming › how to change int to int64 in golang? [solved]
How to change int to int64 in Golang? [SOLVED] | GoLinuxCloud
January 8, 2023 - In Golang, it is very simple to convert an int to int64. In today's post, we will show you some simple examples of converting an int to int64. Depending on the platform, an int can be either 32 or 64 bits.
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.

🌐
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.
🌐
Reddit
reddit.com › r/golang › why is int used rather than int64 in this piece of code?
r/golang on Reddit: Why is int used rather than int64 in this piece of code?
January 15, 2016 -

I am a Go beginner and trying to understand more about the language by studying code out in the wild. I have been studying this snippet:

https://github.com/hailocab/go-geoindex/blob/master/queue.go

Overall it makes sense.

But I do not understand why in the queue struct the size and cap names are int and not int64, especially as these are cast to int64 throughout the code?

The other part I do not understand is the usage of something%int64(queue.cap)... I understand that means modulo the cap of the queue, presumably to handle the case something tries to access a queue slot that is beyond the queue's capacity but wouldn't it make more sense just to check if something>queue.cap and error out rather than assign a random queue element modulo the queue.cap?

🌐
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
And to circle back to ints for a second: You should always code as though every int is an int32, because on some CPUs, it will be. If there’s any chance you’ll need it to store a number larger than [math]2^{31}[/math], use an int64.