// Chapter 28 example: a slice of pointers. range copies the pointer, and the // copy still points at the same critter. // Run with: go run exercises/28/03-pointer-slice.go package main import "fmt" type critter struct { name string energy int } func newCritter(name string, energy int) *critter { return &critter{name: name, energy: energy} } func (c *critter) play() { c.energy = c.energy - 1 fmt.Printf("%s plays. Energy: %d\n", c.name, c.energy) } func main() { critters := []*critter{ newCritter("Spark", 3), newCritter("Bolt", 2), } for _, c := range critters { c.play() } for _, c := range critters { c.play() } fmt.Println(critters[0].energy) // 1 fmt.Println(critters[1].energy) // 0 }