Start learning free

LESSON 126 OF 671 · DICTIONARIES

Accessing Dictionary Values in Go

Lesson

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.

STEP 1 OF 1
    Ctrl + Enter to check