create a map with a key of type string in go

In Go, you can create a map using the built-in make function. Here is an example of how to create a map with a key of type string:

main.go
myMap := make(map[string]int)
30 chars
2 lines

This creates a new map with string keys and integer values. You can then add values to the map using the key-value syntax:

main.go
myMap["apple"] = 1
myMap["banana"] = 2
39 chars
3 lines

You can also retrieve values from the map using the key:

main.go
fmt.Println(myMap["apple"]) // output: 1
41 chars
2 lines

Keep in mind that maps in Go are not ordered, so the order will not be preserved. Also, if you try to access a key that does not exist, the value will be the zero value for the type (in this case, 0).

related categories

gistlibby LogSnag