Go by Example: Pointers

Pointers are a type in Go that hold the memory address of a value. By using pointers, you can modify the value stored at that memory address, which can be useful for a variety of tasks, such as passing values to functions by reference, creating linked data structures, and more.

Here’s an example of how to use pointers in Go:

package main

import "fmt"

func zero(x *int) {
	*x = 0

func main() {
	x := 5
	fmt.Println("x:", x)


x: 0

In this example, the zero function takes a pointer x to an int. The * operator is used to dereference the pointer, allowing the value stored at the memory address to be modified. In the body of the zero function, *x is set to 0, effectively zeroing the value of x.

In the main function, x is declared and initialized to 5. The & operator is used to get the memory address of x, and this address is passed to the zero function as an argument. As a result, the value of x is changed to 0 within the zero function and is also reflected in the main function after the call to zero.

It’s important to be cautious when working with pointers, as it is possible to cause unintended side effects by modifying values stored at memory addresses. However, used correctly, pointers can be a powerful tool in your programming toolkit.

In Go, pointers can be used to pass values to functions by reference instead of by value. When passing values by reference, the function can modify the original value, and the changes will be reflected in the calling code. When passing values by value, the function receives a copy of the value and any changes made to it will not be reflected in the calling code.

Pointers can also be used to create linked data structures, such as linked lists and trees, by pointing one structure to another. For example, you can use pointers to create a linked list where each node has a pointer to the next node in the list.

Another use case of pointers is to reduce the amount of memory used by large data structures. For example, when multiple structures refer to the same data, you can use pointers to avoid duplicating the data and instead store a reference to the shared data.

When working with pointers, it’s important to keep track of the memory addresses you are working with and to dereference pointers correctly. Improper use of pointers can result in undefined behavior, such as null pointer exceptions, and can cause your program to crash.

Go also provides a built-in garbage collector that automatically frees memory that is no longer in use, so you do not need to worry about freeing memory yourself. However, this also means that you do not have full control over the lifecycle of memory, so it’s important to be aware of the trade-offs involved when working with pointers.

In conclusion, pointers are a powerful tool in Go, and can be used for a variety of purposes, such as passing values to functions by reference, creating linked data structures, and reducing memory usage. However, it’s important to use pointers with care, and to be aware of the trade-offs involved, in order to write safe and efficient code.


Leave a Reply

Your email address will not be published. Required fields are marked *