Check if a map contains a key in Go

To check if a map contains a key in Go follow the below steps

  1. Index the map with the given key.
  2. When we index a map in Go, we will get two return values.
  3. First return value contains the value and the second return value is a boolean that indicates if the key exists or not.

We will go through an example to understand it further.

package main

import (
	"fmt"
)

func main() {

	//creating a map
	goMap := map[string]int{"key1": 100, "key2": 200, "key3": 300}

	//Indexing the map with the key.
	value, isMapContainsKey := goMap["key2"]
	//isMapContainsKey will be true if the key contains in goMap
	if isMapContainsKey {
		//key exist
		fmt.Println("Map contains the key and the value is =  ", value)
	} else {
		//key does not exist
		fmt.Println("Map does not contains the key")
	}
}

Now we will run the program.

go run check-if-map-contains-key.go

//OUTPUT
Map contains the key and the value is 200

The above code snippet is more readable and easy to understand.

The if statement in Go can contain both condition and an initialization statement.

We can rewrite the the above code into one single if statement as shown below.

if value, isMapContainsKey := goMap["key"]; isMapContainsKey {
		//key exist
		fmt.Println("Map contains the key and the value is =  ", value)
	} else {
		//key does not exist
		fmt.Println("Map does not contains the key")
}

//OUTPUT
Map does not contains the key.

If your purpose is to check only if the map contains key and if you don’t care about the value, you can use the blank identifier (_“underscore variable”) in place of first return value (i.e., value variable).

if _ , isMapContainsKey := goMap["key2"]; isMapContainsKey {
		//key exist
		fmt.Println("Map contains the key")
	} else {
		//key does not exist
		fmt.Println("Map does not contains the key")
}

//OUTPUT
Map contains the key
Avatar

Arunkumar Gudelli

I am One among a million Software engineers of India. I write beautiful markup.I make the Web useful.

Follow us @ twitter, facebook and linkedin For latest news and articles about Go Language

☝ ✍ Go Language Tutorial ✍☝
Get a short & sweet Go Language tutorials delivered to your inbox every couple of days. No spam ever. Unsubscribe any time.