Creating Maps with `make`
Part of the Fundamentals section of Coddy's GO journey. Lesson 86 of 109.
The make function provides another way to create maps, especially when you don't need to initialize values immediately.
Create an empty map with make:
scores := make(map[string]int)
fmt.Println(scores)Result:
map[]Add elements to the map after creation:
scores["Alice"] = 95
scores["Bob"] = 80
fmt.Println(scores)Result:
map[Alice:95 Bob:80]Challenge
BeginnerIn this challenge, you'll practice creating a map using the make function in Go. You'll create a map that stores the favorite colors of different people.
Your task is to create a map using make and then add some key-value pairs to it.
Try it yourself
package main
import "fmt"
func main() {
// TODO: Create a map called 'favoriteColors' using make
// The keys should be strings (names) and the values should also be strings (colors)
// Adding some key-value pairs to the map
favoriteColors["Alice"] = "Blue"
favoriteColors["Bob"] = "Green"
favoriteColors["Charlie"] = "Red"
// Print the map
fmt.Println("Favorite colors:", favoriteColors)
// Print Bob's favorite color
fmt.Println("Bob's favorite color is", favoriteColors["Bob"])
}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 Decisions9Pointers
What is a Pointer?Declaring Pointer VariablesThe Address-Of OperatorDereferencing PointersUsing Pointers in FunctionsNil PointersRecap - Understanding Pointers12Composite Types: Maps
Introduction to MapsDeclaring Map LiteralsCreating Maps with `make`Adding and Updating Key-ValueAccessing Map ValuesChecking for Key ExistenceDeleting Map EntriesMap LengthIterating Over MapsRecap - Key-Value StoragePractice on your own: Online Go compiler