What Is the Difference Between int, int32, and int64 in Go

int is platform-dependent, while int32 and int64 are fixed-width types ensuring consistent size across all systems.

The difference is that int is platform-dependent (32-bit on 32-bit systems, 64-bit on 64-bit systems), while int32 and int64 are fixed-width types regardless of the architecture. Use int for general counting and indexing, and use int32 or int64 when you need a specific size for binary protocols or file formats.

package main

import (
	"fmt"
	"unsafe"
)

func main() {
	fmt.Printf("int size: %d bits\n", unsafe.Sizeof(int(0))*8)
	fmt.Printf("int32 size: %d bits\n", unsafe.Sizeof(int32(0))*8)
	fmt.Printf("int64 size: %d bits\n", unsafe.Sizeof(int64(0))*8)
}