TL ; DR:
A for loop with range visits every key of a map, together with its value.
stock := map[string]int{"apples": 5, "pears": 2}
for fruit, amount := range stock {
fmt.Println(fruit, amount)
}
// pears 2
// apples 5 (in any order)
Full lesson:
A loop can visit every entry of a map, one at a time. The loop body runs once per key.
range hands the loop each key together with its value. The key comes first, then the value.
Leave out the second variable to get only the keys.
for fruit := range stock {
fmt.Println(fruit, stock[fruit])
}
Don't need the key? Write _ in its place, as in for _, amount := range stock. Go refuses to compile a variable you never use.
The order: Go visits a map in a random order, on purpose. Two runs of the same program can print different orders. A total or a count comes out the same in any order. Those are safe answers.
Your task
A game keeps each player's points in scores. The variables total and count start at 0.
Loop over scores. Add every player's points to total. Count the players with 10 points or more in count.
The loop needs only the points, not the names. The code at the bottom prints both.
Expected output:
43
2Free plan: 30 tutor messages. PRO is unlimited.
