Pointer Address-Of And Dereference

& takes an address, * reads or writes through it, and a nil pointer must be checked before dereferencing or the program panics at runtime.

GoData Integrity

Code

gosnippet
24 lines
package main

import "fmt"

func bump(p *int) {
	// dereference: mutate the caller's value through the pointer
	*p += 10
}

func main() {
	level := 42

	// address-of: &level takes the pointer
	p := &level
	*p = 7 // the write lands directly on level
	fmt.Println("after write via pointer:", level, *p)

	bump(&level)
	fmt.Println("after bump:", level, *p)

	// a zero-value pointer is nil: dereferencing nil panics, so check before use
	var empty *int
	fmt.Println("nil pointer:", empty == nil)
}

How it works

Pointer Address-Of And Dereference reduces to the functions bump and main.

Caveats

  • It carries its own context — no imports beyond the runtime’s own surface.

Using it

Signature: func bump(p *int)

The demo output is labeled (after write via pointer:, after bump:, nil pointer:), so every line in the printed output names the step that produced it.

Execute the file and main walks through bump, in that order.

The key statements that remain

func bump(p *int) {
	// dereference: mutate the caller's value through the pointer
	*p += 10
}

Interactive Tools for This Topic

FAQ

What does Pointer Address-Of And Dereference print when run?
No setup needed: run the file and look for labeled lines like `after write via pointer:`, `after bump:`, `nil pointer:` in the output.