Skip to content

Commit 5f8af0e

Browse files
authored
Merge pull request #96 from devlights/issue-#44-map-delete
Add map delete example ( close #44 )
2 parents 97782f6 + 44a9ee5 commit 5f8af0e

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

basic/map_/map_delete.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package map_
2+
3+
import "fmt"
4+
5+
func MapDelete() error {
6+
7+
var (
8+
f = func(v map[string]int) {
9+
fmt.Printf("len: %d\tval: %v\n", len(v), v)
10+
}
11+
12+
m = map[string]int{
13+
"a": 100,
14+
"b": 200,
15+
}
16+
)
17+
18+
f(m)
19+
20+
// map から要素を削除する場合 組み込み関数 delete() を使う
21+
delete(m, "a")
22+
23+
f(m)
24+
25+
delete(m, "b")
26+
27+
f(m)
28+
29+
// 存在しない要素を delete に渡してもエラーにはならない。何も起きないだけ。
30+
delete(m, "a")
31+
32+
f(m)
33+
34+
return nil
35+
}

lib/mapping.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ func (m SampleMapping) MakeMapping() {
4444
m["map_basic"] = map_.MapBasic
4545
m["map_for"] = map_.MapFor
4646
m["map_initialize"] = map_.MapInitialize
47+
m["map_delete"] = map_.MapDelete
4748
m["scope01"] = scope.Scope01
4849
m["async01"] = async.Async01
4950
m["reflection01"] = reflection.Reflection01

0 commit comments

Comments
 (0)