Golang uint type
last modified May 8, 2025
This tutorial explains how to use the uint
built-in type in Go.
We'll cover unsigned integer basics with practical examples of uint usage.
The uint type represents unsigned integers in Go. Unlike signed integers, uint values are always positive. They provide a larger positive range than their signed counterparts at the same bit size.
In Go, uint
is architecture-dependent, matching either 32 or 64
bits. For specific sizes, use uint8, uint16, uint32, or uint64. Uint values
cannot represent negative numbers.
Basic uint declaration and usage
The simplest use of uint
declares variables for positive numbers.
This example shows basic uint declaration and arithmetic operations.
Note: Uint is ideal for counts, sizes, and indices.
package main import "fmt" func main() { var a uint = 42 var b uint = 13 sum := a + b diff := a - b product := a * b fmt.Println("Sum:", sum) fmt.Println("Difference:", diff) fmt.Println("Product:", product) // Division with uint quotient := a / b remainder := a % b fmt.Println("Quotient:", quotient) fmt.Println("Remainder:", remainder) }
The example demonstrates basic arithmetic with uint values. All operations produce uint results. Negative results from subtraction would wrap around.
Uint size variations
Go provides uint types of specific sizes. This example shows the different uint types and their maximum values.
package main import ( "fmt" "math" ) func main() { var u8 uint8 = math.MaxUint8 var u16 uint16 = math.MaxUint16 var u32 uint32 = math.MaxUint32 var u64 uint64 = math.MaxUint64 fmt.Println("uint8 max:", u8) fmt.Println("uint16 max:", u16) fmt.Println("uint32 max:", u32) fmt.Println("uint64 max:", u64) // Platform-dependent uint size var u uint = math.MaxUint fmt.Println("uint max (platform dependent):", u) }
Each uint type has a specific maximum value. The plain uint
type
matches the platform's word size. Math constants help identify maximum values.
Uint in array indexing
Uint values are commonly used for array and slice indices. This example demonstrates safe array access with uint indices.
package main import "fmt" func main() { data := []string{"apple", "banana", "cherry", "date"} // Using uint for indices var index uint = 2 fmt.Println("Element at index", index, ":", data[index]) // Safe iteration with uint for i := uint(0); i < uint(len(data)); i++ { fmt.Printf("data[%d] = %s\n", i, data[i]) } // Converting between int and uint intIndex := 3 uintIndex := uint(intIndex) fmt.Println("Converted index access:", data[uintIndex]) }
Uint indices prevent negative values that would cause runtime panics. The example shows safe iteration and type conversion between int and uint.
Bitwise operations with uint
Uint types are ideal for bitwise operations. This example demonstrates common bit manipulation techniques with uint values.
package main import "fmt" func main() { var flags uint8 = 0b10101010 // Bitwise operations fmt.Printf("Original: %08b\n", flags) fmt.Printf("AND with 0xF0: %08b\n", flags & 0xF0) fmt.Printf("OR with 0x0F: %08b\n", flags | 0x0F) fmt.Printf("XOR with 0xFF: %08b\n", flags ^ 0xFF) fmt.Printf("NOT: %08b\n", ^flags) // Bit shifting fmt.Printf("Left shift by 2: %08b\n", flags << 2) fmt.Printf("Right shift by 2: %08b\n", flags >> 2) // Bit setting and clearing mask := uint8(1 << 3) flags |= mask // Set bit 3 flags &^= mask // Clear bit 3 }
Uint types support all bitwise operations. The example shows AND, OR, XOR, NOT, and shifting operations. Bit manipulation is precise with uint types.
Uint in system programming
Uint types are essential for system-level programming. This example shows uint usage with file sizes and memory addresses.
package main import ( "fmt" "os" "unsafe" ) func main() { // File size typically uses uint64 fileInfo, _ := os.Stat("example.txt") fileSize := uint64(fileInfo.Size()) fmt.Println("File size:", fileSize, "bytes") // Memory address demonstration var x int = 42 addr := uintptr(unsafe.Pointer(&x)) fmt.Printf("Memory address: 0x%x\n", addr) // System constants often use uint const pageSize uint = 4096 memory := uint(16 * 1024 * 1024 * 1024) // 16GB pages := memory / pageSize fmt.Println("Total pages:", pages) }
System programming often requires uint for sizes and addresses. The example shows file size handling, memory addressing, and page calculations. Uint ensures positive values for these system metrics.
Source
This tutorial covered the uint
type in Go with practical examples
of unsigned integer usage in various contexts.
Author
List all Golang tutorials.