How do you declare and initialize a map in Go?
- var myMap map[string]int
- myMap := make(map[string]int)
- myMap := map[string]int{}
- myMap := new(map[string]int)
In Go, you can declare and initialize a map using the syntax myMap := map[string]int{}. This creates an empty map of type map[string]int. You can also use the make function to create a map with an initial capacity, but the most common way to create a map is with the curly braces {} as shown in option 3. This initializes an empty map that you can populate with key-value pairs.
Loading...
Related Quiz
- What is the primary purpose of Protocol Buffers?
- Describe the role of pointers in memory allocation in Go.
- Explain the difference between sentinel errors and error types in Go.
- In Go, a custom error can be created by implementing the _____ interface.
- In Go, the process of freeing up memory that is no longer needed is handled by the _____.