create a map with a key and value of type string in go

In Go, we can create a map with a key and value of type string with the built-in make function. The make function takes the type of the map as its argument and returns a map of that type. Here's how we can create a map with string keys and values:

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

We can then add key-value pairs to the map using the standard syntax:

main.go
myMap["key1"] = "value1"
myMap["key2"] = "value2"
50 chars
3 lines

We can also access values in the map using the key:

main.go
fmt.Println(myMap["key1"]) // Output: value1
45 chars
2 lines

Maps are a powerful data structure in Go as they provide a way to associate a key with a value. They are used extensively within Go programs for tasks such as caching and routing.

gistlibby LogSnag