Fundamentals 5 min read

Why Go Prevents Pointers from Non‑Pointer Values and What It Means for Safety

This article explains Go's pointer model, why the language forbids generating pointers from non‑pointer values, how garbage collection and memory compaction cause object addresses to change at runtime, and provides best practices and code examples for safely using pointers in Go programs.

Ops Development & AI Practice
Ops Development & AI Practice
Ops Development & AI Practice
Why Go Prevents Pointers from Non‑Pointer Values and What It Means for Safety

Understanding how pointers and object address changes work in Go is essential for writing efficient and reliable code.

1. Basics of Pointers in Go

In Go, a pointer stores the memory address of a variable. Unlike C/C++, Go simplifies pointer usage by removing pointer arithmetic and relies on automatic garbage collection, which reduces manual memory management complexity.

2. Why Object Addresses May Change

During program execution, Go's garbage collector may relocate objects to compact memory, and the runtime may perform memory compression. These mechanisms can cause an object's address to move, meaning the address is not guaranteed to stay constant for the object's lifetime.

3. Why Pointers Cannot Be Created from Non‑Pointer Values

Go prohibits directly converting a non‑pointer value (e.g., an int) into a pointer for two main reasons:

Memory safety : Prevents programmers from referencing invalid memory, which could cause crashes or undefined behavior.

Object movement : Since addresses can change at runtime, creating a pointer from a value that may later move would lead to dangling or incorrect pointers.

Example code demonstrates the correct and incorrect ways to obtain a pointer:

package main

import "fmt"

func main() {
    var a int = 10
    var p *int
    p = &a // Correct: obtain pointer to variable a
    fmt.Println(*p) // prints 10

    // Incorrect example: trying to create a pointer from a non‑pointer value
    // var b int = 20
    // var q *int = b // compile error: cannot use b (type int) as type *int in assignment
}

Using &a to get the address of a is legal, while attempting to assign a non‑pointer value to a pointer variable results in a compilation error.

4. Safe Practices for Using Pointers in Go

Use the address operator ( & ) : Always obtain pointers through the explicit address-of operator.

Avoid pointer escape : When returning a pointer to a local variable, ensure the variable remains alive for the pointer's lifetime to prevent garbage‑collector reclamation.

Understand pointers in slices and maps : Slices and maps internally use pointers; knowing their behavior helps manage memory more effectively.

5. Conclusion

Go enhances memory safety and program stability by restricting how pointers can be created and by managing object address changes through its garbage collector. Mastering these mechanisms is crucial for developing high‑performance, reliable Go applications.

Go pointer illustration
Go pointer illustration
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

GoGarbage CollectionMemory Safetyprogramming fundamentalspointers
Ops Development & AI Practice
Written by

Ops Development & AI Practice

DevSecOps engineer sharing experiences and insights on AI, Web3, and Claude code development. Aims to help solve technical challenges, improve development efficiency, and grow through community interaction. Feel free to comment and discuss.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.