create a map in go

In Go, maps are a built-in data structure that provides a way to store and retrieve key-value pairs. Here's how you can create a map in Go:

main.go
// Create an empty map with string keys and int values
myMap := make(map[string]int)

// Add key-value pairs to the map
myMap["apple"] = 1
myMap["banana"] = 2
myMap["orange"] = 3

// Retrieve the value for a specific key
fmt.Println(myMap["banana"])  // Output: 2
264 chars
11 lines

In the code above, we created an empty map using the make() function with string keys and int values. We then added some key-value pairs to the map and retrieved the value for a specific key using bracket notation. You can also use the delete() function to remove a key-value pair from the map.

gistlibby LogSnag