curena
2/26/2019 - 9:06 PM

Key exists check in Go map

Check if a particular key exists in a map in Go.

myMap := make(map[string]int)

//Assign value to a variable myValue, and whether key exists to a variable keyExists.
myValue, keyExists := myMap["myKey"]
//If the key exists, myValue will have its value. and keyExists will be true.
//Otherwise it'll have value 0 and keyExists will be false.

//If you don't care about the value, you can replace myValue with _
_, keyExists := myMap["myKey"]