0Pricing
Go Academy · Lesson

What Are Pointers?

Address-of operator, dereferencing, nil pointers

What Are Pointers? is a free Go Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Memory Addresses

Every variable lives at a memory address. A pointer stores that address. In Go, pointers are typed: a *int holds the address of an int.

Address-of Operator &

Use & to get the address of a variable:

package main
import "fmt"

func main() {
    x := 42
    p := &x
    fmt.Println(p)  // 0xc000... (address)
    fmt.Println(*p) // 42 (value at address)
}

Dereferencing *

Use * to read or write the value at the pointer's address:

package main
import "fmt"

func main() {
    n := 10
    p := &n
    *p = 99
    fmt.Println(n) // 99
}

nil Pointers

The zero value of a pointer is nil. Dereferencing a nil pointer causes a panic:

package main
import "fmt"

func main() {
    var p *int
    fmt.Println(p)        // <nil>
    fmt.Println(p == nil) // true
    // *p = 1 — PANIC
}

Pointer Types

Each pointer type is specific to what it points to:

package main
import "fmt"

func main() {
    i := 1
    s := "hello"
    pi := &i   // *int
    ps := &s   // *string
    fmt.Printf("%T %T\n", pi, ps) // *int *string
}

Nil Check Before Use

Always check for nil before dereferencing a pointer received from outside your code:

package main
import "fmt"

func printInt(p *int) {
    if p == nil {
        fmt.Println("nil pointer")
        return
    }
    fmt.Println(*p)
}

func main() {
    v := 7
    printInt(&v)
    printInt(nil)
}

Pointers to Structs

Accessing struct fields through a pointer uses the same dot notation — Go auto-dereferences:

package main
import "fmt"

type Point struct{ X, Y int }

func main() {
    p := &Point{1, 2}
    p.X = 10         // same as (*p).X = 10
    fmt.Println(*p)  // {10 2}
}

Pointer Comparison

Two pointers are equal when they point to the same variable (same address) or are both nil:

package main
import "fmt"

func main() {
    a := 1
    p1 := &a
    p2 := &a
    p3 := new(int)
    *p3 = 1
    fmt.Println(p1 == p2) // true  — same address
    fmt.Println(p1 == p3) // false — different address
}

Stack vs Heap

Go's compiler decides whether a variable lives on the stack or heap via escape analysis. When you take the address of a local variable and return it, Go moves it to the heap automatically — you don't manage this manually.

When NOT to Use Pointers

Not everything needs a pointer:

  • Small value types (int, bool, small structs) are cheaper to copy than to dereference
  • Immutable data shared across goroutines is safe as a value
  • Use pointers primarily for mutation or large structs

Quick Check

What does the & operator return?

Recap: Pointers

Key takeaways:

  • &v gets the address; *p dereferences it
  • Zero value is nil — always check before dereferencing
  • Go auto-dereferences struct pointer field access
  • Escape analysis manages stack vs heap automatically

Practice Prompt

Write a function swap(a, b *int) that swaps the values of two integers using pointers. Call it from main and print before/after.

Frequently asked questions

Is the “What Are Pointers?” lesson free?

Yes — the full text of “What Are Pointers?” is free to read here on the web, and the Go Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Go Academy course, upgrade to CoddyKit PRO.

What will I learn in “What Are Pointers?”?

Address-of operator, dereferencing, nil pointers You practise Go Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Go Academy?

No prior experience is required. Go Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “What Are Pointers?” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Go Academy lesson?

Yes. Every Go Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. What Are Pointers?
  2. Pointers and Functions
  3. Pointer Receivers on Methods
  4. new() and When to Use Pointers
← Back to Go Academy