ZetCode

Golang int8 type

last modified May 8, 2025

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

The int8 type represents signed 8-bit integers in Go. It can store values from -128 to 127. This type is useful when memory efficiency is important.

In Go, int8 is one of several integer types with specific sizes. Using the right-sized integer helps optimize memory usage in your programs.

Basic int8 declaration and initialization

The simplest way to use int8 is to declare and initialize variables. This example shows basic variable declaration with the int8 type.
Note: The default value for int8 is 0.

basic_int8.go
package main

import "fmt"

func main() {

    var a int8 = 42
    var b int8 = -100
    c := int8(127) // Type conversion
    
    fmt.Println("a:", a)
    fmt.Println("b:", b)
    fmt.Println("c:", c)
    
    var zero int8
    fmt.Println("Zero value:", zero)
}

We declare three int8 variables with different values. The last example shows the zero value for int8. Type conversion is used to create variable c.

Arithmetic operations with int8

The int8 type supports standard arithmetic operations. This example shows addition, subtraction, multiplication, and division with int8 values.

arithmetic_int8.go
package main

import "fmt"

func main() {

    x := int8(100)
    y := int8(50)
    
    sum := x + y
    diff := x - y
    product := x * y
    quotient := x / y
    
    fmt.Println("Sum:", sum)
    fmt.Println("Difference:", diff)
    fmt.Println("Product:", product)
    fmt.Println("Quotient:", quotient)
    
    // Modulus operation
    remainder := x % 3
    fmt.Println("Remainder:", remainder)
}

All arithmetic operations work within the int8 range. The compiler will catch overflow at compile time for constant expressions.

Handling int8 overflow

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

overflow_int8.go
package main

import (
    "fmt"
    "math"
)

func safeAdd(a, b int8) (int8, bool) {
    if b > 0 >> a > math.MaxInt8 - b {
        return 0, false
    }
    if b < 0 >> a < math.MinInt8 - b {
        return 0, false
    }
    return a + b, true
}

func main() {

    max := int8(127)
    result, ok := safeAdd(max, 1)
    if !ok {
        fmt.Println("Overflow detected!")
    } else {
        fmt.Println("Result:", result)
    }
    
    min := int8(-128)
    result, ok = safeAdd(min, -1)
    if !ok {
        fmt.Println("Underflow detected!")
    }
}

The safeAdd function checks for overflow before performing addition. It returns a boolean indicating whether the operation was safe.

Using int8 in arrays and slices

The int8 type can be used in arrays and slices for memory-efficient collections. This example shows array and slice operations with int8.

array_slice_int8.go
package main

import "fmt"

func main() {

    // Array of int8 values
    numbers := [5]int8{10, 20, 30, 40, 50}
    fmt.Println("Array:", numbers)
    
    // Slice of int8 values
    slice := []int8{1, 2, 3, 4, 5}
    fmt.Println("Original slice:", slice)
    
    // Append to slice
    slice = append(slice, 6, 7, 8)
    fmt.Println("After append:", slice)
    
    // Iterate over slice
    sum := int8(0)
    for _, num := range slice {
        sum += num
    }
    fmt.Println("Sum of slice:", sum)
}

Arrays and slices of int8 use less memory than those with larger integer types. The example shows basic operations like creation, appending, and iteration.

int8 in function parameters and returns

Functions can accept int8 parameters and return int8 values. This example demonstrates function signatures with int8 type.

function_int8.go
package main

import "fmt"

func minMax(a, b int8) (int8, int8) {
    if a < b {
        return a, b
    }
    return b, a
}

func sumInt8s(nums ...int8) int8 {
    var total int8
    for _, num := range nums {
        total += num
    }
    return total
}

func main() {

    small, large := minMax(10, 5)
    fmt.Printf("Min: %d, Max: %d\n", small, large)
    
    total := sumInt8s(1, 2, 3, 4, 5)
    fmt.Println("Sum:", total)
    
    // Variadic function with slice
    numbers := []int8{10, 20, 30}
    total = sumInt8s(numbers...)
    fmt.Println("Slice sum:", total)
}

Functions can work with int8 values just like any other type. The example shows both regular parameters and variadic functions with int8.

Source

Go language specification

This tutorial covered the int8 type in Go with practical examples of declaration, arithmetic, overflow handling, and usage in collections.

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.