Skip to content

Commit 8b3d8c2

Browse files
committed
Add exp_maps.go
1 parent 5858526 commit 8b3d8c2

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

examples/generics/examples.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package generics
22

33
import (
44
"github.com/devlights/try-golang/examples/generics/exp_constraints"
5+
"github.com/devlights/try-golang/examples/generics/exp_maps"
56
"github.com/devlights/try-golang/examples/generics/exp_slices"
67
"github.com/devlights/try-golang/examples/generics/typeconstraints"
78
"github.com/devlights/try-golang/examples/generics/typeparameters"
@@ -24,4 +25,5 @@ func (r *register) Regist(m mapping.ExampleMapping) {
2425
typeconstraints.NewRegister().Regist(m)
2526
exp_constraints.NewRegister().Regist(m)
2627
exp_slices.NewRegister().Regist(m)
28+
exp_maps.NewRegister().Regist(m)
2729
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package exp_maps
2+
3+
import (
4+
"github.com/devlights/try-golang/mapping"
5+
)
6+
7+
type (
8+
register struct{}
9+
)
10+
11+
// NewRegister は、generics パッケージ用の lib.Register を返します.
12+
func NewRegister() mapping.Register {
13+
r := new(register)
14+
return r
15+
}
16+
17+
// Regist は、generics パッケージ配下に存在するサンプルを登録します.
18+
func (r *register) Regist(m mapping.ExampleMapping) {
19+
m["generics_exp_maps"] = ExpMaps
20+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package exp_maps
2+
3+
import (
4+
"github.com/devlights/gomy/output"
5+
"golang.org/x/exp/maps"
6+
)
7+
8+
// ExpMaps -- Go 1.18 リリース時には含まれなかったジェネリクス対応 汎用map処理が定義されている golang.org/x/exp/maps パッケージのサンプルです。
9+
func ExpMaps() error {
10+
var (
11+
m1 = map[string]int{"hello": 100, "world": 101}
12+
m2 = map[string]int{"world": 101, "hello": 100}
13+
k []string
14+
v []int
15+
)
16+
17+
k = maps.Keys(m1)
18+
v = maps.Values(m1)
19+
20+
output.Stdoutl("[maps.Keys]", k, v)
21+
output.Stdoutl("[maps.Values]", maps.Equal(m1, m2))
22+
23+
maps.Clear(m1)
24+
output.Stdoutl("[maps.Clear]", m1, m2)
25+
26+
return nil
27+
}

0 commit comments

Comments
 (0)