Check Key Exists in Map Go Image Check Key Exists in Map Go

In this code snippet, we are going to look at how you can check to see if a key exists within a Map in Go

package main

import "fmt"

func main() {

    mymap := make(map[string]int)

    mymap["elliot"] = 25

    // we can use this if statement to check to see if 
    // a given key "elliot" exists within a map in Go
    if _, ok := mymap["elliot"]; ok {
        // the key 'elliot' exists within the map

When we execute this, we will see the following output:

$ go run main.go

When we execute the call to mymap['key'] we get back two distinct values, the first of which is the value of the key and the second is a bool value which represents whether or not the given key exists within the map.

This second value is what we use to check if a given key exists in the if statement on line 13. We first do an assignment using _, ok := mymap["elliot"]; before then using the ok value as a conditional for our if statement.

Only if the value exists in the map does the body of the if statement execute.

Further Reading

If you found this useful and wish to learn more about Maps in Go, then you may also enjoy this full article on Maps:

Elliot Forbes

Elliot Forbes
Twitter: @Elliot_f

Hey, I'm Elliot and I've been working on TutorialEdge for the last 4 years! If my work has helped you in any way, shape, or form then please consider supporting my work.

become a patron Buy Me A Coffee