Accessing Map Values
Part of the Fundamentals section of Coddy's GO journey — lesson 88 of 109.
To retrieve values from a map, use the key in square brackets. Go also provides a way to check if a key exists.
Create a map of student scores:
students := map[string]int{
"Emma": 92,
"Jack": 78,
}
// Access a value using its key
emmaScore := students["Emma"]
fmt.Println("Emma's score:", emmaScore)Result:
Emma's score: 92Check if a key exists using the two-value assignment:
score, exists := students["Alex"]
fmt.Println("Score:", score, "Exists:", exists)Result:
Score: 0 Exists: falseChallenge
BeginnerIn this challenge, you'll practice accessing values from a map in Go. A map of student grades has been created for you. Your task is to access and print the grade for a specific student.
The map contains student names as keys and their grades as values. You need to retrieve and print the grade for the student named 'Emma'.
Cheat sheet
To retrieve values from a map, use the key in square brackets:
students := map[string]int{
"Emma": 92,
"Jack": 78,
}
// Access a value using its key
emmaScore := students["Emma"]
fmt.Println("Emma's score:", emmaScore)Check if a key exists using the two-value assignment:
score, exists := students["Alex"]
fmt.Println("Score:", score, "Exists:", exists)
// Output: Score: 0 Exists: falseTry it yourself
package main
import "fmt"
func main() {
// A map of student grades
studentGrades := map[string]string{
"John": "B+",
"Emma": "A-",
"Sarah": "A",
"Mike": "C",
}
// TODO: Access and store Emma's grade in a variable called emmaGrade
// TODO: Print Emma's grade with a message like: "Emma's grade is: A-"
}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