Checking Existence
The comma-ok idiom.
Checking Existence is a free Go Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Missing Keys Return Zero
When you read a key that does not exist, Go returns the zero value of the value type, not an error. For int that is 0, for string it is the empty string.
package main
import "fmt"
func main() {
m := map[string]int{"a": 5}
fmt.Println(m["missing"])
}The Ambiguity Problem
If a value of 0 is returned, how do you know whether the key exists with value 0, or is simply absent? You cannot tell from the value alone.
The Comma-Ok Idiom
Map access can return two values: the value and a boolean. The boolean is true if the key exists. This is the comma-ok idiom.
package main
import "fmt"
func main() {
m := map[string]int{"a": 0}
val, ok := m["a"]
fmt.Println(val, ok)
}Distinguishing Absent Keys
Now you can tell the difference. A present key gives ok = true even if its value is the zero value.
package main
import "fmt"
func main() {
m := map[string]int{"a": 0}
_, okA := m["a"]
_, okB := m["b"]
fmt.Println("a exists:", okA, "b exists:", okB)
}Check Before Acting
A common pattern: use comma-ok inside an if to act only when the key is present.
package main
import "fmt"
func main() {
prices := map[string]int{"pen": 3}
if p, ok := prices["pen"]; ok {
fmt.Println("price is", p)
} else {
fmt.Println("not found")
}
}Ignoring the Value
If you only care whether a key exists, use the blank identifier _ for the value.
package main
import "fmt"
func main() {
seen := map[string]bool{"x": true}
_, exists := seen["x"]
fmt.Println("x seen:", exists)
}Counting Occurrences
Comma-ok pairs nicely with counters. But note: even without ok, m[key]++ works because a missing key starts at zero.
package main
import "fmt"
func main() {
counts := map[string]int{}
for _, w := range []string{"a", "b", "a"} {
counts[w]++
}
fmt.Println(counts)
}Default Values Safely
You can provide a fallback when a key is absent using comma-ok and a default variable.
package main
import "fmt"
func main() {
cfg := map[string]string{}
lang, ok := cfg["lang"]
if !ok {
lang = "en"
}
fmt.Println("lang:", lang)
}Comma-Ok Beyond Maps
The two-value form also appears with channel receives and type assertions. The pattern is consistent: the second value reports success.
package main
import "fmt"
func main() {
var i interface{} = "hello"
s, ok := i.(string)
fmt.Println(s, ok)
}A Set Membership Check
Maps with bool or empty-struct values are often used to track membership. Comma-ok cleanly answers is this in the set.
package main
import "fmt"
func main() {
allowed := map[string]struct{}{"read": {}, "write": {}}
_, ok := allowed["delete"]
fmt.Println("delete allowed:", ok)
}Best Practice
Always use comma-ok when a zero value could be a legitimate stored value. It removes ambiguity and makes intent clear.
Quick Check
You read val, ok := m["x"] and get val = 0, ok = true. What does this mean?
Recap
Checking existence with comma-ok:
- Missing keys return the zero value
val, ok := m[key]reports presence inok- Use it whenever zero could be a real value
Frequently asked questions
Is the “Checking Existence” lesson free?
Yes — the full text of “Checking Existence” is free to read here on the web, and the Go Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Go Academy course, upgrade to CoddyKit PRO.
What will I learn in “Checking Existence”?
The comma-ok idiom. You practise Go Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Go Academy?
No prior experience is required. Go Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Checking Existence” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Go Academy lesson?
Yes. Every Go Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Map Internals
- Checking Existence
- Maps as Sets
- Iteration and Ordering