Thuta Learning
AdvancedProgrammingbeginner

Pointers

Relax. We'll talk through this in plain words — no textbook voice.

Pointer stores the memory address where a value lives. In Go, pointers are commonly used when you don't want to copy data, or when you want to change the original value from inside a function.

go
package main

import "fmt"

func updateName(name *string) {
    *name = "ThutaTech"
}

func main() {
    brand := "Old Name"
    updateName(&brand)
    fmt.Println(brand)
}

&brand gives you the address of the brand variable. *name = "ThutaTech" changes the original value the pointer points to.

You should see
ThutaTech

Info

Go doesn't have pointer arithmetic like C/C++. So while you can use pointers, Go still protects you from certain memory-level mistakes.

Easy traps

  • Dereferencing a nil pointer can cause a runtime panic. Check that a pointer isn't nil before using it.