Go Integer Data Types

Welcome to The Coding College! In Go (Golang), integers are a fundamental data type, allowing you to perform arithmetic, indexing, and many other essential operations. Whether you’re building algorithms or managing memory, understanding Go’s integer types is crucial.

This guide will explore the integer types available in Go, their ranges, operations, and best practices.

What Are Integer Data Types?

Integers in Go represent whole numbers without any fractional part. They can be:

  • Signed (positive, negative, or zero).
  • Unsigned (only non-negative values).

Integer Data Types in Go

1. Signed Integers

Signed integers can hold positive and negative values.

TypeSize (bits)Range
int88-128 to 127
int1616-32,768 to 32,767
int3232-2,147,483,648 to 2,147,483,647
int6464-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
int*Platform-specific: 32 or 64 bits depending on the system.

2. Unsigned Integers

Unsigned integers can only hold non-negative values.

TypeSize (bits)Range
uint880 to 255
uint16160 to 65,535
uint32320 to 4,294,967,295
uint64640 to 18,446,744,073,709,551,615
uint*Platform-specific: 32 or 64 bits depending on the system.

Note: The int and uint types depend on the system architecture (32-bit or 64-bit).

3. Special Integer Types

TypeDescription
byteAlias for uint8.
runeAlias for int32, used to represent Unicode characters.
uintptrInteger large enough to hold a pointer.

Declaring Integer Variables

Using Explicit Type

var age int = 25
var smallNumber int8 = -100
var largeNumber uint64 = 18446744073709551615

Using Implicit Type

age := 25 // Compiler infers type as `int`.

Integer Operations

  1. Arithmetic Operators:
    • Addition (+)
    • Subtraction (-)
    • Multiplication (*)
    • Division (/)
    • Modulus (%)

Example:

package main

import "fmt"

func main() {
    a, b := 10, 3
    fmt.Println("Addition:", a+b)
    fmt.Println("Subtraction:", a-b)
    fmt.Println("Multiplication:", a*b)
    fmt.Println("Division:", a/b)
    fmt.Println("Modulus:", a%b)
}

Output:

Addition: 13  
Subtraction: 7  
Multiplication: 30  
Division: 3  
Modulus: 1  
  1. Comparison Operators:
    • Equal to (==)
    • Not equal to (!=)
    • Greater than (>)
    • Less than (<)
    • Greater than or equal to (>=)
    • Less than or equal to (<=)

Type Conversion

Go does not allow implicit type conversion between different integer types. Use explicit conversion.

Example:

package main

import "fmt"

func main() {
    var small int8 = 100
    var large int = int(small) // Explicit conversion
    fmt.Println(large)
}

Example: Working with Integers

Calculating Factorial

package main

import "fmt"

func factorial(n int) int {
    result := 1
    for i := 2; i <= n; i++ {
        result *= i
    }
    return result
}

func main() {
    num := 5
    fmt.Printf("Factorial of %d is %d\n", num, factorial(num))
}

Using Unsigned Integers for Counters

package main

import "fmt"

func main() {
    var counter uint = 0
    for i := 0; i < 5; i++ {
        counter++
        fmt.Println("Counter:", counter)
    }
}

Best Practices for Integer Usage

  1. Choose the Right Type
    • Use int for general-purpose numbers unless specific ranges or memory considerations apply.
  2. Avoid Overflow and Underflow
    • Ensure calculations remain within the type’s range.
  3. Use Unsigned Integers Carefully
    • Only use uint if negative numbers are logically impossible.
  4. Be Explicit with Aliases
    • Use byte and rune only for their intended purposes (e.g., handling characters or raw data).
  5. Handle Division by Zero
    • Ensure the divisor is non-zero to avoid runtime panics.

Conclusion

Go’s integer data types offer flexibility and precision for working with numbers. By understanding their ranges and operations, you can write efficient and safe code.

Leave a Comment