Monthly Archives: June 2020

Go Primer: & and * operators

I have recently enrolled on a Udemy course called GetGoing: Introduction to Golang as I wanted to sink my teeth into the marvellous world of Go; its simplicity and its power to harness concurrency. One of the first concepts you are likely to get confused about when starting to learn Go is the use of the two operators & and *

The operator & is placed in front of a variable and returns its memory address

For example:

myVariable := "aBeautifulString"
anotherVariable :=&myVariable

The result is a memory address, in this fashion: 0xc00008a000

The operator * (aka reference operator) is placed in front of a variable that holds a memory address and resolves it. For example:

aString := "thisIsAString"
anotherString := *&aString

The result will be "thisIsAString"

There is also an additional case where the operator * is placed in front of a type, e.g. *string and it becomes part of the type declaration stating that the variable holds a pointer to a string. For example:

var str *string