/home/josephspurrier

Go Asterisk and Ampersand Cheatsheet

In the Go programming language (Golang), there are two symbols you need to understand when dealing with pointers: the asterisk and ampersand. I’m going to let the code do most of the talking, but there are essentially 4 different ways (illustrated by each of the tests) to pass around variables.

Different Tests with the Asterisk and Ampersand

In each of the tests, the values are outputted three times: when created, when modified by the updatePerson() function, and then after the updatePerson() function.

There also are a few differences to examine between the tests:

Side note: If you are having trouble deciding whether to define methods on values or pointers, check out the Golang FAQs.

A great article to read with a more in-depth explanation is Understanding Pointers and Memory.

#go