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.
Code
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
cURL to Python & Go ConverterTranslate curl CLI commands with headers, auth, JSON bodies, and timeouts into ready-to-run Python requests and Go net/http client code snippets.Email Address ValidatorValidate email syntax, checking the local part, domain labels, and top-level domain against common rules.JSON Pointer EvaluatorEvaluate RFC 6901 JSON Pointer strings against a JSON document to extract the referenced value, with tilde-slash escape handling.MAC Address FormatterNormalize a MAC address from any separator style into colon, hyphen, Cisco dotted, and plain forms, with 12-hex-digit validation and case control.
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.
Report a problem with this exampleOpens a pre-filled issue on GitHub.