Skip to content

Commit 39bd8e4

Browse files
authored
Merge pull request #567 from devlights/add-gocollective-examples
2 parents e8613e9 + ba1f746 commit 39bd8e4

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
version: '3'
2+
3+
tasks:
4+
imports:
5+
cmds:
6+
- goimports -w .
7+
vet:
8+
cmds:
9+
- go vet .
10+
run:
11+
cmds:
12+
- go run -race main.go
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Stackoverflow Go Collective example
2+
//
3+
// How to check if a map contains a key in Go?
4+
//
5+
// URL
6+
// - https://stackoverflow.com/questions/2050391/how-to-check-if-a-map-contains-a-key-in-go
7+
//
8+
// REFERENCES
9+
package main
10+
11+
import "fmt"
12+
13+
func main() {
14+
// Goでmapに対象のキーがあるのかを確認するのは以下のようにする
15+
16+
var (
17+
m = make(map[string]string)
18+
)
19+
m["hello"] = "world"
20+
21+
v, ok := m["hello"]
22+
if !ok {
23+
fmt.Printf("key '%s' does not exists.", "hello")
24+
}
25+
fmt.Println(v)
26+
27+
v, ok = m["world"]
28+
if !ok {
29+
fmt.Printf("key '%s' does not exists.", "world")
30+
}
31+
fmt.Println(v)
32+
}

0 commit comments

Comments
 (0)