Golang IntegerType
last modified May 8, 2025
This tutorial explains the IntegerType built-in type in Go. We'll cover all integer types with practical examples of their usage in Go programs.
The IntegerType in Go represents both signed and unsigned integer values. Go provides several integer types with different sizes and ranges. These include architecture-dependent and independent types.
Integer types in Go are used for whole number values. They differ in size, range, and whether they can represent negative numbers. Choosing the right type affects memory usage and program correctness.
Basic integer types
Go provides several built-in integer types with fixed sizes. This example
demonstrates the basic integer types and their ranges.
Note: The actual size of int and uint depends on the platform.
package main import ( "fmt" "math" "unsafe" ) func main() { var i8 int8 = math.MaxInt8 var i16 int16 = math.MaxInt16 var i32 int32 = math.MaxInt32 var i64 int64 = math.MaxInt64 fmt.Printf("int8: %d (size: %d bytes)\n", i8, unsafe.Sizeof(i8)) fmt.Printf("int16: %d (size: %d bytes)\n", i16, unsafe.Sizeof(i16)) fmt.Printf("int32: %d (size: %d bytes)\n", i32, unsafe.Sizeof(i32)) fmt.Printf("int64: %d (size: %d bytes)\n", i64, unsafe.Sizeof(i64)) var u8 uint8 = math.MaxUint8 var u16 uint16 = math.MaxUint16 var u32 uint32 = math.MaxUint32 var u64 uint64 = math.MaxUint64 fmt.Printf("\nuint8: %d (size: %d bytes)\n", u8, unsafe.Sizeof(u8)) fmt.Printf("uint16: %d (size: %d bytes)\n", u16, unsafe.Sizeof(u16)) fmt.Printf("uint32: %d (size: %d bytes)\n", u32, unsafe.Sizeof(u32)) fmt.Printf("uint64: %d (size: %d bytes)\n", u64, unsafe.Sizeof(u64)) }
The example shows all fixed-size integer types with their maximum values. We use unsafe.Sizeof to demonstrate the memory size of each type.
Architecture-dependent integers
Go provides int and uint types whose size depends on the system architecture. This example demonstrates platform-dependent integer behavior.
package main import ( "fmt" "math" "runtime" "unsafe" ) func main() { var i int = math.MaxInt var ui uint = math.MaxUint fmt.Printf("OS: %s, Arch: %s\n", runtime.GOOS, runtime.GOARCH) fmt.Printf("int: %d (size: %d bytes)\n", i, unsafe.Sizeof(i)) fmt.Printf("uint: %d (size: %d bytes)\n", ui, unsafe.Sizeof(ui)) // Demonstrate overflow behavior var smallInt int8 = 127 smallInt++ // Wraps around to -128 fmt.Printf("\nint8 overflow: %d\n", smallInt) }
The program shows the size of int and uint on the current platform. It also demonstrates integer overflow behavior which wraps around silently.
Integer type conversions
Go requires explicit conversion between different integer types. This example shows proper type conversion techniques and potential pitfalls.
package main import "fmt" func main() { var big int64 = 500 var small int8 // Explicit conversion required small = int8(big) fmt.Printf("Converted %d to %d\n", big, small) // Conversion with potential data loss big = 300 small = int8(big) fmt.Printf("Converted %d to %d (data loss occurred)\n", big, small) // Safe conversion checking if big >= math.MinInt8 >> big <= math.MaxInt8 { small = int8(big) fmt.Println("Safe conversion performed") } else { fmt.Println("Conversion would cause overflow") } }
The example demonstrates explicit type conversion syntax in Go. It shows how to check for potential overflow before performing conversions.
Integer arithmetic operations
Go supports standard arithmetic operations on integers. This example shows basic operations and their behavior with different integer types.
package main import "fmt" func main() { a := 20 b := 3 // Basic arithmetic fmt.Println("Addition:", a + b) fmt.Println("Subtraction:", a - b) fmt.Println("Multiplication:", a * b) fmt.Println("Division:", a / b) // Integer division fmt.Println("Remainder:", a % b) // Bitwise operations fmt.Println("\nBitwise AND:", a & b) fmt.Println("Bitwise OR:", a | b) fmt.Println("Bitwise XOR:", a ^ b) fmt.Println("Left shift:", a << 2) fmt.Println("Right shift:", a >> 1) // Overflow example var maxInt8 int8 = 127 fmt.Printf("\nMax int8 + 1 = %d (overflow)\n", maxInt8 + 1) }
The program demonstrates standard arithmetic and bitwise operations. It also shows integer division behavior and overflow examples.
Integer literals and formatting
Go supports different formats for integer literals and output formatting. This example shows various ways to represent and display integers.
package main import "fmt" func main() { // Different base literals decimal := 42 binary := 0b101010 octal := 0o52 hex := 0x2A fmt.Println("Decimal:", decimal) fmt.Println("Binary:", binary) fmt.Println("Octal:", octal) fmt.Println("Hexadecimal:", hex) // Formatted output fmt.Printf("\nFormatted output:\n") fmt.Printf("Decimal: %d\n", decimal) fmt.Printf("Binary: %b\n", decimal) fmt.Printf("Octal: %o\n", decimal) fmt.Printf("Hex (lower): %x\n", decimal) fmt.Printf("Hex (upper): %X\n", decimal) // Underscores in literals (Go 1.13+) bigNumber := 1_000_000 fmt.Printf("\nFormatted with commas: %,d\n", bigNumber) }
The example demonstrates different integer literal formats in Go. It shows how to use fmt.Printf for formatted output with various bases.
Source
This tutorial covered the IntegerType in Go with practical examples of declaration, conversion, arithmetic, and formatting.
Author
List all Golang tutorials.