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.
Add your answer
Loading...

Leave a comment

Your email address will not be published. Required fields are marked *