Find out how to use pointers in Go and Golang



The facility of C/C++ banks on the usage of pointers. There are blended views of its utility. Some opine it as a superb characteristic whereas others deem it as one thing that’s liable to misuse. Java, though it carries most of the legacy of C++ options, removed pointers express use besides with JNI. Nevertheless, the kind of programming which Java is concerned in doesn’t lack luster with out it. Go, in the meantime, retains some legacy of this C/C++ characteristic however not all. Pointers can do many fascinating issues that in any other case might not be potential. On this article we are going to be taught among the fundamentals of pointer use in Go.

What’s a pointer in Go?

Within the following part, we are going to reply the query: what’s a pointer in Go and the Golang programming language?

A easy variable usually holds values corresponding to intVar :=10 or strVar := “Hi there”. However there are additionally variables that discuss with features, channels, strategies, maps, slices, and so forth. The latter use a kind of variable known as references. A pointer can be a variable nevertheless it holds the reminiscence handle of one other variable. It’s created to level to a selected kind of variable. Which means that the handle of an integer variable kind can solely be saved in a pointer variable declared of the identical kind. This ensures that the Go compiler is aware of the scale of bytes of the pointer to the worth it occupies. Because the handle of the variable is saved in a pointer, we are able to modify the content material of a variable via it.

Learn: Working with Strings in Go.

What’s the level of a pointer in Go?

One explicit want for pointer variables is that they’re low-cost. This implies they occupy a hard and fast dimension whatever the dimension of the worth they level to. The scale of a pointer variable is 8-bytes for 64-bit machines and 4-bytes for 32-bit machines. Think about a string variable which will be of any dimension however the pointer that factors to it’s of fastened dimension. That is necessary as a result of we usually move information to features as arguments. If the operate wants a neighborhood copy of the information, a traditional variable would do. But when we need to alter the worth of the unique variable then as an alternative of passing a variable by its worth we could move by reference via a pointer kind variable. This ensures that even when we’re in several operate scope we are literally pointing to the unique information.

One other side is {that a} variable handed by worth truly creates a duplicate of itself. This occupies reminiscence doubly. With pointers it truly references the unique variable and doesn’t create a duplicate and therefore has much less reminiscence footprint.

Apparently, in Go we are able to enhance the lifetime of a variable unbiased of its scope utilizing pointers. How? The inner reminiscence administration of Go won’t declare the reminiscence so long as there may be no less than one pointer pointing to that variable. (C/C++ would not have an inner reminiscence supervisor, therefore it’s the programmer’s accountability to do all of the clean-ups.)

Examples of Utilizing pointers in Go

The ampersand (&) operator in Go is taken or overloaded not just for binary operation as a bitwise AND operator but additionally as a unary operator to return the reminiscence handle of its operand. Due to this fact the function this operator performs relies upon upon the context of its use. An ampersand(&) put in entrance of a variable means we need to get the handle of the variable.

There may be one other operator: asterisk(*). That is additionally overloaded as a multiplication binary operator and in addition as a pointer dereferencing unary operator. That is used to declare a pointer and in addition used to dereference to the content material of the variable that it factors to. Allow us to illustrate the concepts thus far with a fast instance. Right here is an instance of the best way to use pointers in Go:

bundle important

import (

func important() {

	var a64 int64 // an int64 variable
	var pa64 *int64 = &a64 // handle of int64 variable in a pointer of the identical kind 

	a64 = 1234567890 //dummy worth

	fmt.Println("1. a64 comprises ", a64) 
	fmt.Println("2. a64 handle is ", &a64) 
	fmt.Println("3. pa64 holds the handle of a64, which is ", pa64, " (similar as 2)")
	fmt.Println("4. Accessing the content material of a64 via pa64 ", *pa64, "(similar as 1)")
	fmt.Println("5. Tackle of pa64 ", &pa64)

	var a16 int16
	var pa16 *int16

	a16 = 1256
	pa16 = &a16

	//compiler error: following shouldn't be allowed
	//pa64 = &a16 //can't use &a16 (worth of kind *int16) as *int64 worth in project
	//pa16 = &a64 // can't use &a64 (worth of kind *int64) as *int16 worth in project


	fmt.Println("Pointer pa16 dimension: ", mirror.TypeOf(pa16).Dimension())
	fmt.Println("Pointer pa64 dimension: ", mirror.TypeOf(pa64).Dimension())

Coming into this code and working it in your built-in improvement surroundings (IDE) or code editor leads to the next output:

1. a64 comprises  1234567890
2. a64 handle is  0xc00010c000
3. pa64 holds the handle of a64, which is  0xc00010c000  (similar as 2)
4. Accessing the content material of a64 via pa64  1234567890 (similar as 1)
5. Tackle of pa64  0xc000102018
Pointer pa16 dimension:  8
Pointer pa64 dimension:  8

Observe within the code above how we are able to entry the content material of the variable utilizing asterisk(*) because the dereference operator. We are able to modify the content material as follows:

a64 = 10
fmt.Println("a64 = ", a64)
fmt.Println("modified a64 = ", a64)

This leads to the next output:

a64 =  10
modified a64 =  11

Word: An uninitialized pointer shops nil worth.

var pi *int
if pi == nil {
	fmt.Println("uninitialized pointer")

A pointer, nonetheless, doesn’t need to level to the identical variable and will be set to level to a distinct variable supplied the variable is of the identical kind.

Find out how to Use Multilevel pointers in Golang

It is usually potential to have tips that could pointers (double pointers) and the indirection can go on tips that could tips that could pointers…(triple or extra). Word that we not often want such multilevel pointers and issues can flip messy with them. A sensible recommendation is to suppose twice earlier than declaring double or extra pointers. Go maintains the legacy of C/C++ and has the availability. (Another excuse Go typically doesn’t want multi stage pointers is because of reference kind variables). A fast instance on the best way to use double pointers:

bundle important

import "fmt"

func important() {

	x := 10 //dummy worth
	px := &x //assigned handle of a variable
	ppx := &px //assigned handle of one other pointer

	fmt.Println("x =", x, ",px =", *px, ",ppx =", **ppx)
	*px = *px * 2
	fmt.Println("x =", x, ",px =", *px, ",ppx =", **ppx)
	**ppx = **ppx + 5
	fmt.Println("x =", x, ",px =", *px, ",ppx =", **ppx)

Operating this creates the next output:

x = 10 ,px = 10 ,ppx = 10
x = 20 ,px = 20 ,ppx = 20
x = 25 ,px = 25 ,ppx = 25

Pointers as Operate Receivers

When a operate has a pointer kind argument we are able to ship the handle of the variable. Any modifications made to the variable within the operate truly displays the modifications to the unique variable. A traditional instance is the swap operate, given under.

bundle important

import "fmt"

func localSwap(this, that string) {
	maintain := this
	this = that
	that = maintain

func realSwap(this, that *string) {
	maintain := *this
	*this = *that
	*that = maintain

func important() {

	this := "this"
	that := "that"

	fmt.Println("this = ", this, "that = ", that)
	localSwap(this, that)
	fmt.Println("After swap: this = ", this, "that = ", that) //no change
	realSwap(&this, &that)
	fmt.Println("After actual swap: this = ", this, "that = ", that)

Operating this within the code editor provides us the next outcome:

this =  this that =  that
After swap: this =  this that =  that
After actual swap: this =  that that =  this

Observe that, as we invoke the localSwap operate, the variables are handed by worth. This merely implies that a duplicate of the worth is handed to the operate. Because of this, any modifications made to them regionally within the localSwap operate does probably not mirror any change within the authentic worth. Within the latter case, as we invoke the realSwap operate, the variables aren’t handed by worth however by reference. We ship the reminiscence handle of the variable and these reminiscence addresses are caught by the pointer variable operate argument of the realSwap operate. Due to this fact any modifications made with the dereference operator on the pointer, the unique variable will get modified. That is the rationale why the localSwap operate is unable to make any modifications though the realSwap does with the identical logic.

Learn: Strategies in Go Defined.

Go and Golang Tutorials

The usage of pointers in Go is safer compared to C/C++ because of computerized reminiscence administration. Right here, clearing reminiscence after we’re completed with a pointer shouldn’t be a difficulty, however with C/C++ it’s, and have to be taken care of explicitly. In Go, so long as no less than one pointer refers to a variable, the reminiscence for it isn’t reclaimed. That is additionally the rationale why it’s secure to return tips that could native variables which can have been created inside a operate in Go. However that is notably a foul concept in C/C++ besides static variables.

Learn extra Go and Golang programming tutorials for builders.