The Fundamentals of Pointers in Go | by Nikhil Vaidyar | Feb, 2022

Understanding pointer types and how to use them in data structures

Nikhil Vaidyar
`A “Marketplatz cafe” by Michael Parzuchowski on Unsplash

So, what are pointers and why do we need to use them?

A pointer is a variable that stores a memory address. In simple terms, it stores the value in your computer memory. Though later we can access it using pointer address.

The address operator (&) simply determines the address of a variable in memory. Suppose, you assign some value to a variable. Your computer will store that value in your memory address(RAM) as a hexadecimal number.

value := 901
fmt.Println(value)
//Prints -> 0xc000018030
value := 65
memoryAdd := &value
fmt.Println("Value:", value) // 65
fmt.Println("Memory address:", &memoryAdd) // 0xc000100018
fmt.Println("Dereferencing using pointer:", *memoryAdd) // 65
var name string = "John"
var pointer *string = &name
fmt.Println("Name =", name) //John
fmt.Println("pointer =", pointer) //0xc000010230
fmt.Println("*pointer =", *pointer) //John
*pointer = "Robert Downy Junior"fmt.Println("*pointer =", *pointer) //Robert Downy Jr
fmt.Println("name =", name) //Robert Downy Jr

So, in our case pointer variable assigned to a new value which is

types := 881
address := &types
fmt.Printf("Address is a %Tn", address) // Address is a *int

https://go.dev/play/p/PFBD3_5BnLP What if we define pointers using data structures in go which are like arrays, slices, maps, etc., and what go compiler will return in that time? So, let’s get to know more about pointers in a bit deep.

Leave a Comment