summaryrefslogtreecommitdiffhomepage
path: root/go.html.markdown
diff options
context:
space:
mode:
authorMi-Br <43519102+Mi-Br@users.noreply.github.com>2024-05-17 15:28:58 +0200
committerGitHub <noreply@github.com>2024-05-17 07:28:58 -0600
commit21c588354c88b309408b81e738efc6829c9b9c5b (patch)
tree5767c3c80258931ed9d00648ea7fd2adf66b18b1 /go.html.markdown
parentc268b08f13a9265581a6be3224af0792a587d48d (diff)
[go/en] missing map keys (#4413)
Diffstat (limited to 'go.html.markdown')
-rw-r--r--go.html.markdown7
1 files changed, 7 insertions, 0 deletions
diff --git a/go.html.markdown b/go.html.markdown
index b93cbc61..81d45b98 100644
--- a/go.html.markdown
+++ b/go.html.markdown
@@ -150,6 +150,13 @@ can include line breaks.` // Same string type.
// hash or dictionary types of some other languages.
m := map[string]int{"three": 3, "four": 4}
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.
// The underscore lets you "use" a variable but discard its value.