To find the value associated with a key in a Go map, we can simply use the following syntax:
main.go22 chars2 lines
Here, yourMap
is the map we want to look up and key
is the key for which we want to find the associated value. If the key is present in the map, its corresponding value will be returned in value
. Otherwise, the zero value of the map's value type will be returned.
Here's an example that demonstrates how to find a value associated with a key in a Go map:
main.go310 chars20 lines
In this example, we create a map myMap
with string keys and integer values. We then look up the value associated with the key "bar"
by using the indexing syntax with the square brackets. The value 4
is returned and printed to the console.
gistlibby LogSnag