1
0
mirror of https://github.com/adambard/learnxinyminutes-docs.git synced 2025-08-06 14:56:54 +02:00

[go/en] missing map keys (#4413)

This commit is contained in:
Mi-Br
2024-05-17 15:28:58 +02:00
committed by GitHub
parent c268b08f13
commit 21c588354c

View File

@@ -150,6 +150,13 @@ can include line breaks.` // Same string type.
// hash or dictionary types of some other languages. // hash or dictionary types of some other languages.
m := map[string]int{"three": 3, "four": 4} m := map[string]int{"three": 3, "four": 4}
m["one"] = 1 m["one"] = 1
// Looking up a missing key returns the zero value,
// which is 0 in this case, since it's a map[string]int
m["key not present"] // 0
// Check if a key is present in the map like this:
if val, ok := m["one"]; ok {
// Do something
}
// Unused variables are an error in Go. // Unused variables are an error in Go.
// The underscore lets you "use" a variable but discard its value. // The underscore lets you "use" a variable but discard its value.