Go > Core Go Basics > Fundamental Data Types > Unsigned Integers (uint, uint8, uint16, uint32, uint64)
Working with Unsigned Integers in Go
This snippet demonstrates how to declare, initialize, and use unsigned integer types in Go (uint
, uint8
, uint16
, uint32
, uint64
). Unsigned integers are non-negative integers, making them suitable for representing counts, sizes, or memory addresses.
Declaration and Initialization
This code declares and initializes several unsigned integer variables. uint8
stores values from 0 to 255, uint32
stores larger values, and uint64
even larger ones. The `uint` type's size depends on the system's architecture (32-bit or 64-bit). The reflect.TypeOf()
function is used to display the actual data type of the variables. The last part demonstrates what happens when an unsigned integer overflows, it wraps around to 0.
package main
import (
"fmt"
"reflect"
)
func main() {
// Declaration and initialization of unsigned integers
var age uint8 = 30 // Unsigned 8-bit integer (0 to 255)
var fileSize uint32 = 1024000 // Unsigned 32-bit integer (0 to 4294967295)
var population uint64 = 7000000000 // Unsigned 64-bit integer (0 to 18446744073709551615)
var index uint = 10 // Unsigned integer (architecture-dependent size)
fmt.Printf("Age: %d (Type: %s)\n", age, reflect.TypeOf(age))
fmt.Printf("File Size: %d (Type: %s)\n", fileSize, reflect.TypeOf(fileSize))
fmt.Printf("Population: %d (Type: %s)\n", population, reflect.TypeOf(population))
fmt.Printf("Index: %d (Type: %s)\n", index, reflect.TypeOf(index))
// Example of overflow (uint8)
var overflow uint8 = 255
overflow++ // Increment beyond the maximum value
fmt.Printf("Overflow: %d (Type: %s)\n", overflow, reflect.TypeOf(overflow))
}
Concepts Behind Unsigned Integers
Unsigned integers are whole numbers that cannot be negative. They are useful when you know that a value will never be negative, such as representing a count, size, or memory address. Because they don't need to represent negative values, they can store larger positive values than their signed counterparts (int
, int8
, etc.).
Real-Life Use Case
Unsigned integers are commonly used in scenarios where negative values are not possible or do not make sense. For example:
Best Practices
Interview Tip
Be prepared to explain the difference between signed and unsigned integers, and when each type should be used. Understand the implications of overflow and how it can be handled in your code. Mention real-world examples of where unsigned integers are commonly used, such as representing file sizes or memory addresses.
When to Use Them
Use unsigned integers when:
Memory Footprint
The memory footprint of each unsigned integer type is as follows:
Choosing the appropriate size can help optimize memory usage, especially when dealing with large arrays or data structures.uint8
: 1 byteuint16
: 2 bytesuint32
: 4 bytesuint64
: 8 bytesuint
: Architecture-dependent (usually 4 bytes on 32-bit systems, 8 bytes on 64-bit systems)
Alternatives
If negative values are possible, use signed integer types (int
, int8
, int16
, int32
, int64
). If you need arbitrary-precision integers, consider using the math/big
package.
Pros
Cons
FAQ
-
What happens if I assign a negative value to an unsigned integer?
Assigning a negative value to an unsigned integer will result in a compile-time error if the value is a constant. If the value is determined at runtime, it will be converted to its unsigned equivalent, which is a very large positive number due to the way two's complement works. This can lead to unexpected behavior, so it's crucial to avoid assigning negative values to unsigned integers. -
What is the difference between
uint
andint
?
uint
is an unsigned integer type, whileint
is a signed integer type.uint
can only represent non-negative values, whileint
can represent both positive and negative values. The specific number of bits used byuint
andint
depends on the architecture of the system (32-bit or 64-bit). -
How do I handle overflow in unsigned integers?
Unsigned integers wrap around when they exceed their maximum value. For example, if auint8
is 255 and you increment it, it will become 0. To handle this, you can use modular arithmetic or check if the result of an operation exceeds the maximum value before assigning it to the unsigned integer.