Kodewick
Start learning →
Basic←→0 / 70

Everyone's a year older

#054BasicStructsSlices4 min

It's New Year, and every pet is a year older. Add 1 to the Age of each pet in pets; the loop below shows the new ages. Careful: the p in for _, p := range pets is a copy, so changing it doesn't change the slice.

Should show
Bean 3
Tofu 6
Rex 10
main.go
package main

import "fmt"

type Pet struct {
	Name string
	Age  int
}

func main() {
	pets := []Pet{{"Bean", 2}, {"Tofu", 5}, {"Rex", 9}}
	// your code here: with for i := range pets, add 1 to each Age
	for _, p := range pets {
		fmt.Println(p.Name, p.Age)
	}
}
Ctrl + Enter runs too
Result

Run tries the examples. When they look right, Submit.