ZetCode

Golang uint8 type

last modified May 8, 2025

This tutorial explains how to use the uint8 built-in type in Go. We'll cover basic concepts with practical examples of working with 8-bit unsigned integers.

The uint8 type represents an 8-bit unsigned integer in Go. It can store values from 0 to 255. This type is commonly used for memory efficiency when small positive numbers are sufficient.

In Go, uint8 is one of several unsigned integer types. It's particularly useful for binary data, pixel values, and other cases where negative numbers aren't needed.

Basic uint8 declaration and usage

The simplest use of uint8 involves variable declaration and basic arithmetic. This example demonstrates fundamental operations.
Note: uint8 values cannot be negative.

basic_uint8.go
package main

import "fmt"

func main() {

    var a uint8 = 200
    var b uint8 = 55
    
    sum := a + b
    diff := a - b
    product := a * b
    
    fmt.Println("Sum:", sum)
    fmt.Println("Difference:", diff)
    fmt.Println("Product:", product)
    
    // This would cause overflow
    // overflow := a * b * 2
    // fmt.Println("Overflow:", overflow)
}

The example shows basic arithmetic with uint8 values. Note that operations resulting in values outside 0-255 will overflow silently.

Working with uint8 and byte type

In Go, uint8 is aliased to the byte type. This example demonstrates their interchangeability and common uses.

uint8_byte.go
package main

import "fmt"

func main() {

    // uint8 and byte are equivalent
    var u uint8 = 'A'
    var b byte = 65
    
    fmt.Printf("uint8: %c, byte: %c\n", u, b)
    
    // Working with byte slices
    data := []byte{'H', 'e', 'l', 'l', 'o'}
    fmt.Println("Byte slice:", string(data))
    
    // Converting between types
    var num uint8 = 123
    var char byte = byte(num)
    fmt.Printf("Number: %d, Character: %c\n", num, char)
}

The example shows how uint8 and byte can be used interchangeably. This is particularly useful when working with binary data or ASCII characters.

Handling uint8 overflow

Since uint8 has a limited range, overflow can occur. This example demonstrates how to detect and handle potential overflow situations.

uint8_overflow.go
package main

import (
    "fmt"
    "math"
)

func safeAdd(a, b uint8) (uint8, bool) {
    if math.MaxUint8 - a < b {
        return 0, false // Overflow would occur
    }
    return a + b, true
}

func main() {

    var x uint8 = 200
    var y uint8 = 100
    
    // Regular addition that would overflow
    result1 := x + y
    fmt.Println("Overflowed result:", result1) // 44 (200 + 100 - 256)
    
    // Safe addition with overflow check
    result2, ok := safeAdd(x, y)
    if !ok {
        fmt.Println("Addition would overflow!")
    } else {
        fmt.Println("Safe result:", result2)
    }
}

The safeAdd function checks for potential overflow before performing the addition. This pattern helps prevent subtle bugs in arithmetic operations.

Using uint8 in arrays and slices

uint8 is commonly used in arrays and slices for compact data storage. This example shows practical applications.

uint8_slices.go
package main

import "fmt"

func main() {

    // Pixel data (RGB values)
    pixels := [3]uint8{255, 128, 0}
    fmt.Printf("Pixel: R=%d, G=%d, B=%d\n", 
        pixels[0], pixels[1], pixels[2])
    
    // Compact data storage
    data := []uint8{0x48, 0x65, 0x6c, 0x6c, 0x6f}
    fmt.Println("Message:", string(data))
    
    // Processing uint8 slices
    var sum uint16
    for _, val := range data {
        sum += uint16(val)
    }
    fmt.Println("Sum of bytes:", sum)
}

The example demonstrates uint8's use in pixel data and compact storage. Note the conversion to uint16 when summing to prevent overflow.

Bit manipulation with uint8

uint8 is ideal for bit-level operations due to its small size. This example shows common bit manipulation techniques.

uint8_bits.go
package main

import "fmt"

func main() {

    var flags uint8 = 0
    
    // Set bits
    flags |= 1 << 0 // Set bit 0
    flags |= 1 << 2 // Set bit 2
    
    fmt.Printf("Flags: %08b\n", flags)
    
    // Check bits
    if flags & (1 << 2) != 0 {
        fmt.Println("Bit 2 is set")
    }
    
    // Clear bits
    flags &^= 1 << 0 // Clear bit 0
    fmt.Printf("Flags after clear: %08b\n", flags)
    
    // Toggle bits
    flags ^= 1 << 3 // Toggle bit 3
    fmt.Printf("Flags after toggle: %08b\n", flags)
}

The example demonstrates setting, checking, clearing, and toggling individual bits in a uint8 value. This is useful for compact flag storage.

Source

Go language specification

This tutorial covered the uint8 type in Go with practical examples of declaration, arithmetic, overflow handling, and bit manipulation.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all Golang tutorials.