Tutorials Logic, IN info@tutorialslogic.com

Golang Pointers: Addresses, Dereferencing and Pointer Receivers

Address and Dereference

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.

  • `&value` means address of value.
  • `*ptr` means value stored at the pointer address.
  • A nil pointer points to nothing and cannot be safely dereferenced.
  • Use pointers when a function needs to mutate a caller-owned value.

Modify Through Pointer

Modify Through Pointer
package main

import "fmt"

func applyDiscount(price *int, amount int) {
	*price = *price - amount
}

func main() {
	total := 1000
	applyDiscount(&total, 150)
	fmt.Println(total) // 850
}

Pointer Receivers

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.

  • Value receivers receive a copy of the struct.
  • Pointer receivers can update the original struct.
  • Keep receiver style consistent for a type when possible.
  • Maps, slices, and channels already contain reference-like internals, but structs often need pointer receivers for mutation.

Pointer Receiver Method

Pointer Receiver Method
type Counter struct {
	Value int
}

func (c *Counter) Increment() {
	c.Value++
}

func main() {
	counter := Counter{}
	counter.Increment()
	fmt.Println(counter.Value) // 1
}

When Not to Use Pointers

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.

  • Do not use pointers only to look advanced.
  • Avoid returning pointers to shared mutable state unless ownership is clear.
  • Check nil before dereferencing optional pointer values.
  • Prefer clear data ownership over clever pointer passing.

Nil Pointer Guard

Nil Pointer Guard
func printName(name *string) {
	if name == nil {
		fmt.Println("name is missing")
		return
	}
	fmt.Println(*name)
}
Before you move on

Golang Pointers: Addresses, Dereferencing and Pointer Receivers Mastery Check

5 checks
  • 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.
  • 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.

Golang Pointers Questions Learners Ask

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.

Next Step
Next Practice

Finish the concept here, then reinforce it with hands-on coding, interview prep, or a tool that matches the topic.

Browse Free Tutorials

Explore 500+ free tutorials across 20+ languages and frameworks.