ZetCode

Golang int type

last modified May 8, 2025

This tutorial explains how to use the int built-in type in Go. We'll cover integer basics with practical examples of working with numbers.

The int type represents signed integer values in Go. Its size is platform-dependent, being 32 bits on 32-bit systems and 64 bits on 64-bit systems. Go also provides explicitly sized integer types like int8, int16, etc.

In Go, int is the most commonly used integer type. It's suitable for most integer operations and provides good performance across platforms. The range of values depends on the system architecture.

Basic integer declaration and initialization

The simplest use of int involves declaring and initializing variables. This example demonstrates basic integer operations.
Note: Go has strict typing, so integers can't mix with other types.

basic_int.go
package main

import "fmt"

func main() {

    var a int = 42
    var b int = 13
    
    sum := a + b
    difference := a - b
    product := a * b
    quotient := a / b
    remainder := a % b
    
    fmt.Println("Sum:", sum)
    fmt.Println("Difference:", difference)
    fmt.Println("Product:", product)
    fmt.Println("Quotient:", quotient)
    fmt.Println("Remainder:", remainder)
}

The example shows basic arithmetic operations with integers. Go performs integer division when both operands are integers, discarding any remainder. The modulus operator (%) returns the division remainder.

Integer type conversion

p> Go requires explicit type conversion between different numeric types. This example demonstrates converting between int and other numeric types.

type_conversion.go
package main

import (
    "fmt"
    "math"
)

func main() {

    var i int = 42
    var f float64 = float64(i)
    var u uint = uint(math.Abs(float64(i)))
    
    fmt.Printf("int: %d, float64: %f, uint: %d\n", i, f, u)
    
    // Converting back to int
    f = 3.14
    i = int(f)
    fmt.Println("Float to int:", i) // Truncates decimal part
}

The example shows conversions between int, float64, and uint. Note that converting from float to int truncates the decimal part without rounding. Explicit conversion is required even between compatible numeric types.

Integer overflow and underflow

Go integers have fixed sizes and can overflow or underflow. This example demonstrates integer limits and overflow behavior.

overflow.go
package main

import (
    "fmt"
    "math"
)

func main() {

    maxInt := math.MaxInt64
    minInt := math.MinInt64
    
    fmt.Println("Max int64:", maxInt)
    fmt.Println("Min int64:", minInt)
    
    // Overflow example
    overflow := maxInt + 1
    fmt.Println("Overflow result:", overflow) // Wraps around
    
    // Underflow example
    underflow := minInt - 1
    fmt.Println("Underflow result:", underflow) // Wraps around
}

The example shows integer limits and overflow behavior. When an integer overflows or underflows, it wraps around to the opposite limit. This is important to consider in arithmetic operations to prevent unexpected results.

Bitwise operations with integers

Go provides comprehensive bitwise operations for integers. This example demonstrates common bit manipulation techniques.

bitwise.go
package main

import "fmt"

func main() {

    var a int = 0b1100 // Binary literal (12 in decimal)
    var b int = 0b1010 // Binary literal (10 in decimal)
    
    fmt.Printf("a: %b (%d)\n", a, a)
    fmt.Printf("b: %b (%d)\n", b, b)
    
    // Bitwise operations
    and := a & b
    or := a | b
    xor := a ^ b
    shiftLeft := a << 2
    shiftRight := a >> 1
    
    fmt.Printf("AND: %b (%d)\n", and, and)
    fmt.Printf("OR: %b (%d)\n", or, or)
    fmt.Printf("XOR: %b (%d)\n", xor, xor)
    fmt.Printf("Shift left: %b (%d)\n", shiftLeft, shiftLeft)
    fmt.Printf("Shift right: %b (%d)\n", shiftRight, shiftRight)
}

The example shows various bitwise operations. Go supports AND, OR, XOR, and bit shifting operations. Binary literals (0b prefix) make bit patterns more readable in code.

Integer parsing and formatting

Converting between strings and integers is common in programming. This example demonstrates parsing strings to integers and formatting integers.

parse_format.go
package main

import (
    "fmt"
    "strconv"
)

func main() {

    // String to int
    numStr := "42"
    num, err := strconv.Atoi(numStr)
    if err != nil {
        fmt.Println("Conversion error:", err)
        return
    }
    fmt.Println("Parsed number:", num)
    
    // Int to string
    result := strconv.Itoa(num * 2)
    fmt.Println("Formatted result:", result)
    
    // Parse with base
    hexStr := "1a"
    hexNum, err := strconv.ParseInt(hexStr, 16, 64)
    if err != nil {
        fmt.Println("Hex conversion error:", err)
        return
    }
    fmt.Printf("Hex %s = %d\n", hexStr, hexNum)
}

The example shows string-to-int conversion with error handling and different number bases. The strconv package provides robust functions for parsing and formatting integers with proper error handling.

Source

Go language specification

This tutorial covered the int type in Go with practical examples of arithmetic operations, type conversion, 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.