Sobes.tech
Junior — Middle

How can you determine if a Map contains a specific key-value pair?

sobes.tech AI

Answer from AI

In Go, to check if a key exists in a map, the operation of retrieving the value with a check of the second return value — a boolean flag indicating whether the key was found — is used.

Example:

m := map[string]int{"a": 1, "b": 2}

value, ok := m["a"]
if ok {
    fmt.Println("Key found, value:", value)
} else {
    fmt.Println("Key not found")
}

Here, ok will be true if the key "a" exists in the map, and false if it does not.

How can you determine if a Map contains a specific… - sobes.tech