-
Book Overview & Buying
-
Table Of Contents
Practical Systems Programming in Go
By :
Pointers are a fundamental feature in Go that allow you to directly reference and manipulate memory locations. Unlike some higher-level languages that abstract away memory management, Go gives you controlled access to pointers, enabling more efficient and flexible code without sacrificing safety. This subsection explores how pointers work in Go, including how to declare them, dereference them, and use them to modify data indirectly. You will also learn how Go handles memory allocation with the new and make functions, and how pointers interact with structs, functions, and slices.
The first example of the use of pointers is a simple one, as it demonstrates pointer usage for passing variables by reference to enable functions to modify the original variable's value.
package main
import "fmt"
func main() {
x := 10
fmt.Println("Before:", x)
// Pass the address of x to the function
updateValue(&x)
fmt.Println("After:", x...