In Go, you can get the number of key-value pairs in a map using the built-in len() function:
scores := map[string]int{}
scores["Alice"] = 95
scores["Bob"] = 87
scores["Charlie"] = 72
n := len(scores)
fmt.Println(n) // 3
len() returns the number of entries. You can also check if it's empty by comparing to zero:
fmt.Println(len(scores) == 0) // false
empty := map[string]int{}
fmt.Println(len(empty)) // 0
Assignment
Follow the Coding Tutorial and get the number of entries in scores!
Hint
Use len(scores) and assign it to count.
Free plan: 30 tutor messages. PRO is unlimited.
Saved
Ctrl + Enter to check
