×

go · level 2 · unit 2 · lesson 2

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

A says where a box lives, not what is inside.

When you hand a value to a function, Go hands over a copy. The function can change its copy as much as it likes, and your original box stays the same. Sometimes that is exactly what you do not want.

Every box lives at an , like a house number. &score gives that address, and a value holding an address is a pointer. Its type has a star: &score is an *int, a pointer to an int.

Put a star in front of a pointer to reach the box it points at, so *p = 10 changes the original. Remember fmt.Scan(&answer)? You handed Scan a pointer, so it could fill your box.

package main
import "fmt"
func addBonus(points *int) {
	*points = *points + 10
}
func main() {
	score := 5
	addBonus(&score)
	fmt.Println(score)
	p := &score
	*p = 100
	fmt.Println(score)
}

In a type, *int means pointer to an int, while in code *p means the box that p points at.