Go: int vs. int64

An int is either 32 or 64 bits depending on the platform. The length of an array can always be represented by an int.

  • An index, a length or a capacity should normally be an int.

  • The types int8, int16, int32, and int64 are best suited for data.
    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:

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]

Here is an example from the time package:

// 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? shows how to compute the size and limit values of an int as untyped constants.


Be the first to comment!