Start learning free

LESSON 128 OF 671 · DICTIONARIES

Dictionary Length in Go

Lesson

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.

STEP 1 OF 1
    Ctrl + Enter to check