Address and Dereference Operators
Start with Address and Dereference Operators
The address operator & and the dereference operator * are complementary: one produces a pointer and the other accesses its target.
What you’ll learn
- Explain the main purpose of address and dereference operators.
- Read and run the complete Go example in this lesson.
- Apply the concept and recognize common mistakes.
Understand the idea
With value := 12, &value is a *int. Writing *(&value) reads the original integer. Once a pointer is valid, assigning to its dereferenced expression changes the target.
The operator symbols depend on context: *int means a pointer type, *p means dereference, and a * b means multiplication. Go requires an addressable operand for the ordinary address-of operation.
Visualize how it works
Follow this diagram before executing the example. Check where a value or decision changes what runs next.
Run a complete Go example
This example is a complete program. Save it as main.go or paste it into the Go Playground.
Go code
package main
import "fmt"
func main() {
count := 12
location := &count
fmt.Println("Read:", *location)
*location += 3
fmt.Println("After change:", count)
fmt.Println("Addressable value:", *(&count))
}
Output
Read: 12
After change: 15
Addressable value: 15Trace what the program does
Taking &count produces a pointer to count. Dereferencing it prints 12, and incrementing through that pointer changes count to 15. Dereferencing &count reads the same 15.
Avoid common mistakes
- The address-of operator does not produce a second copy of the value.
- The dereference operator requires a valid non-nil pointer.
Try it yourself
Use & and * with a float64 variable, then increase the original value through the pointer.
Tip: Edit the example above, predict the output, and run the modified code to test your reasoning.
Check your understanding
What you should remember
You have practiced Address and Dereference Operators in a small, self-contained program. The important habit is to predict what runs, inspect the actual output and distinguish the language rule from the particular example.
Before continuing, make sure you can explain the program to someone who has not seen it, including the line that changes its result.
Reaching the recap and completing the quizzes are separate milestones.