This program creates an inventory map with two items. It adds a new item 'eraser' and updates the count of 'pen'. Then it prints all items and their counts.
Go
package main
import"fmt"
func main() {
inventory := map[string]int{"pen": 10, "notebook": 5}
// Add a new item
inventory["eraser"] = 3// Update existing item
inventory["pen"] = 15
fmt.Println("Inventory:")
for item, count := range inventory {
fmt.Printf("%s: %d\n", item, count)
}
}
OutputSuccess
Important Notes
If you try to update a key that does not exist, Go will add it automatically.
Maps in Go do not keep order, so items may print in any order.
Summary
Use mapName[key] = value to add or update values in a map.
Adding a new key creates a new pair; updating changes the existing value.
Maps help organize data with easy access and modification.
Practice
(1/5)
1. What happens when you assign a value to a new key in a Go map using mapName[key] = value?
easy
A. A new key-value pair is added to the map.
B. The map is deleted.
C. The map becomes empty.
D. The program crashes.
Solution
Step 1: Understand map assignment
Assigning a value to a new key in a map adds that key with the value.
Step 2: Differentiate from update
If the key exists, the value updates; if not, a new pair is created.
Final Answer:
A new key-value pair is added to the map. -> Option A
Quick Check:
Adding new key = new pair added [OK]
Hint: Assigning to new key adds pair, not error or delete [OK]
Common Mistakes:
Thinking map clears on new key assignment
Confusing adding with deleting
Assuming program crashes on new key
2. Which of the following is the correct syntax to update the value for key "age" to 30 in a Go map named person?
easy
A. person["age"] := 30
B. person["age"] = 30
C. person.age = 30
D. person.update("age", 30)
Solution
Step 1: Recall map update syntax
In Go, map values are updated by assigning with = operator: map[key] = value.
Step 2: Check each option
person["age"] = 30 uses correct syntax. person["age"] := 30 uses := which is for variable declaration, not map update. person.age = 30 uses dot notation which is invalid for maps. person.update("age", 30) uses a method that does not exist.
Final Answer:
person["age"] = 30 -> Option B
Quick Check:
Use = to update map value [OK]
Hint: Use =, not := or dot, to update map values [OK]
Common Mistakes:
Using := instead of = for map update
Using dot notation for map keys
Trying to call update method on map
3. What is the output of this Go code?
m := map[string]int{"a": 1, "b": 2}
m["a"] = 5
fmt.Println(m["a"])
medium
A. 1
B. 2
C. 5
D. 0
Solution
Step 1: Analyze initial map values
The map m starts with keys "a":1 and "b":2.
Step 2: Update value for key "a"
m["a"] = 5 changes the value for "a" from 1 to 5.
Step 3: Print updated value
fmt.Println(m["a"]) prints the updated value 5.
Final Answer:
5 -> Option C
Quick Check:
Updated value prints 5 [OK]
Hint: Updated map key prints new value, not old [OK]
Common Mistakes:
Expecting old value instead of updated
Confusing key "b" value with "a"
Thinking print shows zero value
4. Identify the error in this Go code snippet that tries to add a new key-value pair to a map:
var m map[string]int
m["new"] = 10
medium
A. Key "new" is a reserved word and cannot be used.
B. Syntax error: missing semicolon after map declaration.
C. Value 10 must be a string, not int.
D. Map m is nil and not initialized before adding a key.
Solution
Step 1: Understand map initialization
Declaring var m map[string]int creates a nil map. It must be initialized with make() before adding keys.
Step 2: Identify error cause
Assigning m["new"] = 10 on a nil map causes runtime panic because map is not initialized.
Final Answer:
Map m is nil and not initialized before adding a key. -> Option D
Quick Check:
Maps must be initialized before adding keys [OK]
Hint: Always initialize maps with make() before adding keys [OK]
Common Mistakes:
Assuming var declares initialized map
Thinking key "new" is invalid
Confusing value types for map
5. Given a map inventory := map[string]int{"apple": 5, "banana": 3}, write the correct Go code to add 2 more apples to the inventory without overwriting the existing count.
hard
A. inventory["apple"] += 2
B. inventory["apple"] = inventory["apple"] - 2
C. inventory["apple"] = 2
D. inventory.add("apple", 2)
Solution
Step 1: Understand current value
Current count for "apple" is 5 in the map.
Step 2: Add 2 without overwriting
Use += operator to add 2 to existing value: inventory["apple"] += 2.
Step 3: Check other options
inventory["apple"] = 2 overwrites with 2, not add. inventory["apple"] = inventory["apple"] - 2 subtracts 2. inventory.add("apple", 2) uses a non-existent method.
Final Answer:
inventory["apple"] += 2 -> Option A
Quick Check:
Use += to add to map value [OK]
Hint: Use += to add to existing map value, not = [OK]
Common Mistakes:
Overwriting value instead of adding
Subtracting instead of adding
Using non-existent map methods
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.