A Go pointer stores the memory address of a value. Pointers let functions modify existing values, avoid copying large structs, represent optional references, and define methods that mutate receiver state.
Go pointers are simpler than C pointers because Go does not allow pointer arithmetic in normal code. You use `&` to take an address and `*` to dereference a pointer. A pointer can also be `nil`, so nil checks are important before dereferencing uncertain values.
The `&` operator gets the address of a variable. The `*` operator reads or writes the value at that address. If two variables point to the same value, a change through one pointer affects the original value.
package main
import "fmt"
func applyDiscount(price *int, amount int) {
*price = *price - amount
}
func main() {
total := 1000
applyDiscount(&total, 150)
fmt.Println(total) // 850
}
Methods can use value receivers or pointer receivers. Use a pointer receiver when the method should modify the struct or when copying the struct would be expensive.
type Counter struct {
Value int
}
func (c *Counter) Increment() {
c.Value++
}
func main() {
counter := Counter{}
counter.Increment()
fmt.Println(counter.Value) // 1
}
Pointers are useful, but unnecessary pointers can make code harder to read. Small immutable values such as ints, booleans, and short structs can often be passed by value.
func printName(name *string) {
if name == nil {
fmt.Println("name is missing")
return
}
fmt.Println(*name)
}
Not in normal safe Go code. Go pointers reference values but do not support C-style arithmetic.
No. Use pointer receivers for mutation, large structs, or consistency when some methods require pointers.
A value receiver gets a copy of the struct, so changes made inside the method affect only that copy. A pointer receiver gets the address of the original value, so field assignments update the caller’s struct.
Practice, interview questions, and compiler links for Golang Pointers.
Finish the concept here, then reinforce it with hands-on coding, interview prep, or a tool that matches the topic.
Explore 500+ free tutorials across 20+ languages and frameworks.
Fresh tutorials, interview guides, and coding practice in your inbox.