How to check if a map contains a key in Go?

15 Jul, 2021, 1 min read

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

Discord

Join our mailing list

We will reach out when exciting new posts are available. We won’t send you spam. Unsubscribe at any time.