0
0
Goprogramming~10 mins

Adding and updating values in Go - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to add a new key-value pair to the map.

Go
package main

import "fmt"

func main() {
	ages := map[string]int{"Alice": 30}
	ages["Bob"] = [1]
	fmt.Println(ages)
}
Drag options to blanks, or click blank then click option'
Aage
B"25"
C25
Dnil
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string value instead of an integer.
Using an undefined variable.
2fill in blank
medium

Complete the code to update the value for the key "Alice" in the map.

Go
package main

import "fmt"

func main() {
	ages := map[string]int{"Alice": 30, "Bob": 25}
	ages["Alice"] = [1]
	fmt.Println(ages)
}
Drag options to blanks, or click blank then click option'
Aage
B35
C"35"
Dnil
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string instead of an integer.
Trying to update a key that does not exist.
3fill in blank
hard

Fix the error in the code by completing the blank to add a new key-value pair.

Go
package main

import "fmt"

func main() {
	ages := map[string]int{"Alice": 30}
	ages[[1]] = 40
	fmt.Println(ages)
}
Drag options to blanks, or click blank then click option'
A"Bob"
BBob
C40
Dage
Attempts:
3 left
💡 Hint
Common Mistakes
Using an unquoted string as a key.
Using a value instead of a key.
4fill in blank
hard

Fill both blanks to add a new key-value pair and update an existing one.

Go
package main

import "fmt"

func main() {
	ages := map[string]int{"Alice": 30, "Bob": 25}
	ages[[1]] = 40
	ages[[2]] = 35
	fmt.Println(ages)
}
Drag options to blanks, or click blank then click option'
A"Charlie"
B"Alice"
C"Bob"
D"David"
Attempts:
3 left
💡 Hint
Common Mistakes
Using keys without quotes.
Trying to update a key that does not exist.
5fill in blank
hard

Fill all three blanks to add a new key-value pair, update an existing one, and print a value from the updated map.

Go
package main

import "fmt"

func main() {
	ages := map[string]int{"Alice": 30, "Bob": 25}
	ages[[1]] = 40
	ages[[2]] = 35
	fmt.Println(ages[[3]])
}
Drag options to blanks, or click blank then click option'
A"Charlie"
B"Bob"
C"Alice"
D"David"
Attempts:
3 left
💡 Hint
Common Mistakes
Using keys without quotes.
Printing a key that was not added or updated.