What is type uintptr in Golang?

Key takeaways:

  • Golang is a statically typed language, meaning variable types are defined explicitly or inferred by the compiler.

  • uintptr is an integer type capable of storing the bit pattern of any pointer.

  • It is used in unsafe operations, converting unsafe.Pointer to perform arithmetic, and back to unsafe.Pointer.

  • Variables of uintptr type can store and print memory address values but doesn’t reference objects.

  • Pointers ensure type safety and prevent garbage collection, while uintptr allowing low-level memory manipulation and are not type-safe.

  • Use uintptr in advanced, performance-critical, or unsafe operations.

Variables contain data, which can be of different data types, or types for short. Golang is a statically typed language, which means the compiler must know the types of all the variables, either because they were explicitly indicated or because the compiler can infer the type from the code context. A type defines the set of values and the set of operations that can take place on those values.

Type uintptr in Golang is an integer type that is large enough to contain the bit pattern of any pointer. In other words, uintptr is an integer representation of a memory address. Below is how you declare a variable of type uintptr:

var var_name uintptr

uintptr is generally used to perform indirect arithmetic operations on unsafe pointers for unsafe memory access. First, unsafe.Pointer is converted to uintptr, and then the arithmetic operation is performed on uintptr. Finally, it is converted back to unsafe.Pointer, which now points to the new memory address as a result of the operation.

uintptr is also used to store and print the pointer address value. Since the variable with the type uintptr does not reference anything as it’s a plain integer, the pointer corresponding to that object can be garbage-collected.

Syntax and usage

Below is an example of how to declare and use a uintptr variable:

var var1 uintptr = uintptr(0xc82000c290)
fmt.Println(var1)
Syntax and usage of uintptr()

In this case, var1 holds a memory address represented as an integer. When printed, the output is the integer value of the address.

Example

package main
import "fmt"
func main() {
var var1 uintptr = 0xc82000c290
fmt.Println("Value of var1:", var1)
fmt.Printf("Type of var1: %T", var1)
}

Explanation

The above code declares the variable var1 with type uintptr, which is assigned the memory address 0xc82000c290. If you run the code, you can observe that the value of var1 is displayed as an integer.

Key differences between pointers and uintptr

The below comparison helps to determine when to use pointers or uintptr depending on the requirements of your program.

Feature

Pointer

uintptr

Purpose

References memory addresses directly.

Represents memory addresses as integers.

Type safety

Type-safe; enforces type constraints.

Not type-safe; treated as an integer.

Memory protection

Prevents referenced memory from being garbage collected.

Does not prevent memory from being garbage collected.

Operations

Used for dereferencing and referencing memory.

Used for arithmetic and manipulation on addresses.

Use case

Preferred for safe memory operations.

Used in unsafe or low-level memory operations.

Garbage collection

Protects the object from garbage collection.

Allows garbage collection as it does not track references.

When to use each

  • Use pointers: When working with memory-safe, type-referenced operations.

  • Use uintptr: For low-level operations requiring memory manipulation or when interacting with unsafe APIs.

If you want to get more info about pointers in Go, please look into our Answer: What are pointers in Golang?

Conclusion

The uintptr type in Golang bridges safe and unsafe memory operations. Its primary use lies in advanced scenarios like interfacing with hardware or optimizing low-level performance-critical code.

Discover the Go for DevOps course—designed to help you harness the full potential of Go in automating workflows, improving system reliability, and enhancing performance for modern DevOps environments.

Frequently asked questions

Haven’t found what you were looking for? Contact Us


What is `uintptr` in Go?

uintptr is an integer type in Go large enough to hold the bit pattern of any pointer. It is used for low-level memory operations and converting unsafe.Pointer for arithmetic, but it is not type-safe.


What is interface type in Golang?

An interface in Go specifies a method set and allows dynamic polymorphism. It defines a contract that types must satisfy to be considered its implementation.

You can get more info in our Answer: How to use Interfaces in GO


What are unnamed types in Golang?

Unnamed types are types without explicit identifiers, like anonymous structs or function types, often declared inline.


What is the difference between `uintptr` and unsafe pointers?

uintptr is an integer used for pointer arithmetic, while unsafe.Pointer is a type for type-agnostic pointer manipulation. Unlike unsafe.Pointer, uintptr does not protect memory from garbage collection.


Free Resources

Copyright ©2025 Educative, Inc. All rights reserved