Map Length
Part of the Fundamentals section of Coddy's GO journey — lesson 91 of 109.
The len function returns the number of key-value pairs in a map. This helps you determine how many entries a map contains.
Create a map of country populations:
populations := map[string]int{
"USA": 331,
"India": 1380,
"China": 1441,
}
count := len(populations)
fmt.Println("Number of countries:", count)The result shows the map's length:
Number of countries: 3An empty map has length zero:
emptyMap := map[string]int{}
fmt.Println("Empty map length:", len(emptyMap))Challenge
BeginnerIn this challenge, you'll practice using the len() function to find the number of key-value pairs in a map.
We've created a map called countries that contains some countries and their capitals. Your task is to use the len() function to find out how many countries are in the map, and then print that number.
Cheat sheet
The len function returns the number of key-value pairs in a map:
populations := map[string]int{
"USA": 331,
"India": 1380,
"China": 1441,
}
count := len(populations)
fmt.Println("Number of countries:", count) // Output: Number of countries: 3An empty map has length zero:
emptyMap := map[string]int{}
fmt.Println("Empty map length:", len(emptyMap)) // Output: Empty map length: 0Try it yourself
package main
import "fmt"
func main() {
// A map of countries and their capitals
countries := map[string]string{
"France": "Paris",
"Japan": "Tokyo",
"Brazil": "Brasília",
"Canada": "Ottawa",
"Egypt": "Cairo",
}
// TODO: Use the len() function to get the number of countries in the map
// and store it in a variable called countryCount
// Print the number of countries
fmt.Printf("The map contains %d countries\n", countryCount)
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Comparison & Logical Operators
Comparison Operators - Part 1Comparison Operators - Part 2Logical AND OperatorLogical OR OperatorLogical NOT OperatorOperator Precedence BasicsRecap - Making Comparisons7Control Flow: Loops
What The `for` Loop ExplainedFor Loop - BasicFor Loop - Condition OnlyThe `break` KeywordThe `continue` KeywordNested LoopsRecap - Repeating Actions2Variables and Basic Data Types
What is a variableType Inference with `:=`Integers (int)Floating-Point NumbersBooleansStringsZero ValuesConstantsNaming ConventionsRecap - Variables and Types5Basic Input/Output
Formatted OutputFormat VerbsPrinting TypesGetting Basic User InputRecap - Input and Output8Functions
Understanding FunctionsDeclaring a FunctionCalling FunctionsFunction ParametersReturning a Single ValueReturning Multiple ValuesNamed Return ValuesFunction Scope BasicsRecap - Creating Reusable Code3Basic Operators
Arithmetic OperatorsDivision OperatorThe Modulo OperatorAssignment OperatorAugmented Assignment OperatorsIncrement and DecrementRecap - Calculations6Control Flow: Conditionals
The `if` StatementThe `else` KeywordThe `else if` KeywordVariable Shadowing in `if`Initializing VariablesThe `switch` StatementSwitch with ExpressionsSwitch without ExpressionThe `fallthrough` KeywordRecap - Making Decisions