Pointers are not unique to Go, but Go has a unique flavour of pointers, so it’s good to learn why they are unique, as you’ll use pointers in every Go program.
Everything in Go is passed by value, as in C and in all languages descending from C.
If you pass an Int to a function, the variable is copied.
If you pass a pointer, the pointer is copied, but not the value it points to.
So, in most cases pointers are used to call functions and pass parameters by reference.
Let’s take a simple variable
x, defined as an integer with value
x := 5
Now, the memory address of
x can be obtained with
If you print
fmt.Println, you’ll see that it’s something like
0xc42000e248: a memory address.
Now, we can assign this value to a variable, and that variable will be a pointer to
y := &x
If we write this assignment statement
*y = 10
x value will now be
No pointer arithmetic
Go pointers are similar to C and C++ pointers, but they are safer, as you cannot perform operations like
var x *int; x++;
on a pointer, because the address of a pointer cannot be altered. No pointer/array duality.
Returning pointers of local function variables
Local function variables referenced by returned pointers will remain available while the pointer is available. This will make sure the pointer is always pointing to what you think.
The zero value of a pointer is
nil. This means that a nil pointer is still possible, but this causes fewer issues than in other languages (C, Java, C++) because there are less types masked as pointers, and less other nils laying around. For example, strings in C are pointers. In Go, they are a type.
nil is not a common return for functions, like in single return value languages — error checking is more organized, and
nil is not a common zero value. Many built-in data types (strings, arrays, maps, slices..) have zero values different than
More go tutorials:
- Using NGINX Reverse Proxy to serve Go services
- Making a copy of a struct in Go
- The basics of a Go Web Server
- Sorting a map type in Go
- Go pointers in a nutshell
- Go Tags explained
- Go Date and Time Formatting
- JSON processing with Go
- Go Variadic Functions
- Go Strings Cheat Sheet
- The Go Empty Interface Explained
- Debugging Go with VS Code and Delve
- Named Go returns parameters
- Generating random numbers and strings in Go
- Filesystem Structure of a Go project
- Binary Search Algorithm Implemented in Go
- Using Command Line Flags in Go
- GOPATH Explained
- Build a Command Line app with Go: lolcat
- Building a CLI command with Go: cowsay
- Using Shell Pipes with Go
- Go CLI tutorial: fortune clone
- List the files in a folder with Go
- Use Go to get a list of repositories from GitHub
- Go, append a slice of strings to a file
- Go, convert a string to a bytes slice
- Visualize your local Git contributions with Go
- Getting started with Go CPU and memory profiling
- Solving the "does not support indexing" error in a Go program
- Measuring execution time in a Go program
- Building a Web Crawler with Go to detect duplicate titles
- Go Best Practices: Pointer or value receivers?
- Go Best Practices: Should you use a method or a function?
- Go Data Structures: Set
- Go Maps Cheat Sheet
- Generate implementations for generic types in Go
- Go Data Structures: Dictionary
- Go Data Structures: Hash Table
- Implement Events Listeners in Go through Channels
- Go Data Structures: Stack
- Go Data Structures: Queue
- Go Data Structures: Binary Search Tree
- Go Data Structures: Graph
- Go Data Structures: Linked List
- The complete guide to Go Data Structures
- Comparing Go Values
- Is Go object oriented?
- Working with a SQL Database in Go
- Using environment variables in Go
- Go tutorial: REST API backed by PostgreSQL
- Enabling CORS on a Go Web Server
- Deploying a Go Application in a Docker Container
- Why Go is a powerful language to learn as a PHP developer
- Go, remove the io.Reader.ReadString newline char
- Go, how to watch changes and rebuild your program
- Go, count the months since a date
- Accessing HTTP POST parameters in Go