Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

uint is a built-in unsigned integer type in Go whose size is determined strictly by the target compilation architecture (GOARCH). It represents non-negative whole numbers and is allocated as either 32 bits (e.g., on GOARCH=386 or arm) or 64 bits (e.g., on GOARCH=amd64 or arm64). This size is fixed during compilation, regardless of the host system compiling the code or the operating system executing the resulting binary.

Technical Specifications

  • Memory Footprint: 4 bytes (32-bit) or 8 bytes (64-bit).
  • Value Range (32-bit): 0 to 4,294,967,295 (23212^{32} - 1)
  • Value Range (64-bit): 0 to 18,446,744,073,709,551,615 (26412^{64} - 1)
  • Zero Value: 0

Type System Behavior

Go enforces strict typing. uint is an entirely distinct type from explicitly sized unsigned integers (uint32, uint64) and signed integers (int). Even on a 64-bit architecture where uint and uint64 share the exact same memory footprint and range, the compiler treats them as separate types. Implicit type coercion is strictly prohibited.

Syntax and Operations

Declaration and Initialization

package main

func main() {
    // Explicit declaration
    var x uint = 42

    // Short variable declaration with type conversion from an untyped integer constant
    y := uint(42)

    // Zero value initialization
    var z uint // z is 0
    
    // Blank identifier used to satisfy the compiler's unused variable constraint
    _, _, _ = x, y, z
}

Type Conversion

Because uint is a distinct type, operations involving other integer types require explicit type conversion.
package main

func main() {
    var a uint = 100
    var b uint64 = 200
    var c int = 50

    // Invalid: mismatched types
    // result := a + b 

    // Valid: explicit conversion to uint
    result1 := a + uint(b)

    // Valid: explicit conversion to uint64
    result2 := uint64(a) + b

    // Valid: converting signed to unsigned
    result3 := a + uint(c) 
    
    _, _, _ = result1, result2, result3
}

Determining Architecture Size at Compile Time

You can programmatically reference the bit size of uint for the target architecture using the standard library’s math/bits package. This value is evaluated during compilation, not at runtime.
package main

import (
    "fmt"
    "math/bits"
)

func main() {
    // bits.UintSize is a compile-time constant evaluating to either 32 or 64
    fmt.Printf("The size of uint on the target architecture is %d bits.\n", bits.UintSize)
}

Overflow Behavior

Like all fixed-size integer types in Go, uint wraps around upon overflow or underflow.
package main

func main() {
    var max uint = ^uint(0) // Bitwise NOT on 0 yields the maximum uint value
    max += 1                // Overflows to 0

    var min uint = 0
    min -= 1                // Underflows to the maximum uint value
    
    _, _ = max, min
}
Master Go with Deep Grasping Methodology!Learn More