03 / 08

What is the difference between pass-by-value and pass-by-pointer in Go?

Go is always pass-by-value, but passing a pointer passes the memory address by value, allowing the function to mutate the original data.

Go has no pass-by-reference in the C++ sense. Everything is copied — but when you pass a pointer, the copy is of the address, so the function can follow that address to read or mutate the original value.

Value vs pointer semantics
When to use pointer receivers
  1. 1

    When the method needs to mutate the receiver's fields

  2. 2

    When the struct is large and copying would be expensive

  3. 3

    For consistency — if any method on a type uses a pointer receiver, all methods should

  4. 4

    Use value receivers for small, immutable structs like coordinate points

  5. 5

    Interfaces store a copy for value receivers; pointer receivers require a pointer to satisfy the interface