Complete the code to declare a map that stores string keys and int values.
var ages [1]Maps in Go are declared using the map[keyType]valueType syntax. Here, map[string]int means keys are strings and values are integers.
Complete the code to initialize a map using the make function.
scores := [1](map[string]int)new instead of make for maps.make or a literal.The make function is used to create and initialize maps in Go. It allocates memory for the map.
Fix the error in the code to correctly add a key-value pair to the map.
m := make(map[string]int) m[[1]] = 10
Map keys must be of the declared key type. Since the key type is string, the key must be a string literal like "age".
Fill both blanks to create a map comprehension that stores word lengths for words longer than 3 characters.
lengths := map[string]int{
[1]: len([2]) for _, [2] := range words if len([2]) > 3
}words as a key.In Go, map literals don't support comprehensions like Python. This task is a trick to think about keys and values: keys are strings (words), so use "word" as a key placeholder and word as the variable holding each word.
Fill all three blanks to check if a key exists in the map and print its value.
if val, [1] := m[[2]]; [3] { fmt.Println(val) }
In Go, to check if a key exists in a map, you use the two-value assignment: val, ok := m["key"]. Then you check if ok is true.