How to check if a map contains a key in Go?
How to check if a map contains a key in Go using example- Sriram Thiagarajan
- July 15, 2021
How to check if a map contains a key in Go?
Checking for a particular key from a map variable is a rather common task which can be done using the below pseudo-code
mapvalue, ok := mapvariable[searchKey]
mapvalue
gets the value of the key associated in that map
ok
get a boolean value of true
or false
based on the availability of the searchkey in the map
if ok {
fmt.Println("Map value is " + mapvalue)
} else {
fmt.Println("Map key not found in the list")
}
Check the live code below for a shortcut using if condition :)
That’s it forks for our quick tips today. Check out the rest of the post for more detailed tutorials on Go and other topics
Stay tuned by subscribing to our mailing list and joining our Discord community