From the course: Go for Developers: Practical Techniques for Effective Coding

Unlock this course with a free trial

Join today to access over 24,900 courses taught by industry experts.

Pointers

Pointers

- A pointer is a type that holds the address to another value. People are often afraid of pointers. In other languages, you hear horror stories about pointer arithmetic overriding memory spaces by accident, and other terrible tales. At their core, pointers in Go are very straightforward. To understand pointers, we must first understand how Go passes data. When passing data from one function to another, Go will first copy the data to be passed and then send the copied data to the destination function. This is called pass by value. Here, we have a rough diagram of a Go application and its memory. Each function in Go occupies its own memory space. For example, function A declares a new value of type user. This value lives in function A's stack, with the memory address x123. In another memory stack is function B that takes a value of type user. When we call function B with the user from function A, Go will create a copy of that user in the B function's memory stack. Here, we can see that…

Contents