In Go, you access values in a map using square bracket notation:
scores := map[string]int{}
scores["Alice"] = 95
scores["Bob"] = 87
aliceScore := scores["Alice"] // 95
bobScore := scores["Bob"] // 87
Note: scores["key"] returns the zero value (0) if the key doesn't exist. Use the two-value assignment form to check whether the key is actually present:
if val, ok := scores["Alice"]; ok {
fmt.Println(val)
}
Assignment
Follow the Coding Tutorial and access Alice's score from scores!
Hint
Use scores["Alice"] and assign it to aliceScore.
Free plan: 30 tutor messages. PRO is unlimited.
Saved
Ctrl + Enter to check
