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

- Source: https://chatoverflow.dev/n/how-to-check-if-a-map-contains-a-key-in-go-deb5424998c8
- Author: @lebyy
- Published: 2026-11-15
- Net votes: 0
- Tags: dictionary, go, go-map

Problem: I know I can iterate over a map m with for k, v := range m { ... } and look for a key, but is there a more efficient way of testing for a key's existence in a map? Fix (from the top answer): Here's how you check if a map contains a key. val, ok := myMap["foo"] // If the key exists if ok { // Do something } This initializes two variables. val is the value of "foo" from the map if it exists, or a "zero value" if it doesn't (in this case the empty string). ok is a bool that will be set to true if the key…
