File tree Expand file tree Collapse file tree 9 files changed +173
-6
lines changed
Expand file tree Collapse file tree 9 files changed +173
-6
lines changed Original file line number Diff line number Diff line change 11package generics
22
33import (
4+ "github.com/devlights/try-golang/examples/generics/exp_constraints"
5+ "github.com/devlights/try-golang/examples/generics/exp_maps"
6+ "github.com/devlights/try-golang/examples/generics/exp_slices"
47 "github.com/devlights/try-golang/examples/generics/typeconstraints"
58 "github.com/devlights/try-golang/examples/generics/typeparameters"
69 "github.com/devlights/try-golang/mapping"
@@ -20,4 +23,7 @@ func NewRegister() mapping.Register {
2023func (r * register ) Regist (m mapping.ExampleMapping ) {
2124 typeparameters .NewRegister ().Regist (m )
2225 typeconstraints .NewRegister ().Regist (m )
26+ exp_constraints .NewRegister ().Regist (m )
27+ exp_slices .NewRegister ().Regist (m )
28+ exp_maps .NewRegister ().Regist (m )
2329}
Original file line number Diff line number Diff line change 1+ package exp_constraints
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_constraints" ] = ExpConstraints
20+ }
Original file line number Diff line number Diff line change 1+ package exp_constraints
2+
3+ import (
4+ "github.com/devlights/gomy/output"
5+ "golang.org/x/exp/constraints"
6+ )
7+
8+ type myStr string
9+
10+ func add [E constraints.Signed ](x , y E ) E {
11+ return x + y
12+ }
13+
14+ func less [E constraints.Ordered ](x , y E ) bool {
15+ return x < y
16+ }
17+
18+ // ExpConstraints -- Go 1.18 リリース時には含まれなかった制約型が定義されている golang.org/x/exp/constraints パッケージのサンプルです。
19+ func ExpConstraints () error {
20+ //
21+ // constraints パッケージには、Go 1.18 リリース時には
22+ // 含まれなかった制約型が定義されている。
23+ //
24+ // - Complex
25+ // - Float
26+ // - Integer
27+ // - Ordered
28+ // - Signed
29+ // - Unsigned
30+ //
31+ // 特に Ordered は助かる。
32+ //
33+ output .Stdoutl ("[Signed]" , add (int32 (1 ), int32 (3 )))
34+ output .Stdoutl ("[Signed]" , add (int8 (1 ), int8 (- 2 )))
35+
36+ output .Stdoutl ("[Ordered]" , less (1 , 2 ))
37+ output .Stdoutl ("[Ordered]" , less ("z" , "b" ))
38+ output .Stdoutl ("[Ordered]" , less (myStr ("b" ), myStr ("z" )))
39+
40+ return nil
41+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 1+ package exp_slices
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_slices" ] = ExpSlices
20+ }
Original file line number Diff line number Diff line change 1+ package exp_slices
2+
3+ import (
4+ "github.com/devlights/gomy/output"
5+ "golang.org/x/exp/slices"
6+ )
7+
8+ // ExpSlices -- Go 1.18 リリース時には含まれなかったジェネリクス対応 汎用slice処理が定義されている golang.org/x/exp/slices パッケージのサンプルです。
9+ func ExpSlices () error {
10+ var (
11+ s1 = []string {"hello" , "world" }
12+ s2 = []string {"hello" , "world" }
13+ s3 = []string {"world" , "hello" }
14+ s4 = []int {100 , 101 }
15+ )
16+
17+ output .Stdoutl ("[Equal(s1, s2)]" , slices .Equal (s1 , s2 ))
18+ output .Stdoutl ("[Equal(s2, s3)]" , slices .Equal (s2 , s3 ))
19+ // compile error
20+ //fmt.Println(slices.Equal(s1, s4))
21+
22+ s5 := slices .Insert (s4 , 1 , 999 )
23+ output .Stdoutl ("[Insert]" , s4 , s5 )
24+
25+ idx := slices .Index (s5 , 999 )
26+ s6 := slices .Delete (s5 , idx , idx + 1 )
27+ output .Stdoutl ("[Delete]" , s5 , s6 )
28+
29+ return nil
30+ }
Original file line number Diff line number Diff line change @@ -5,10 +5,11 @@ go 1.18
55require (
66 github.com/devlights/gomy v0.4.6
77 golang.org/x/crypto v0.0.0-20220214200702-86341886e292
8+ golang.org/x/exp v0.0.0-20220518171630-0b5c67f07fdf
89 golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
9- golang.org/x/term v0.0.0-20210927222741-03fcf44c2211
10+ golang.org/x/term v0.0.0-20220411215600-e5f449aeb171
1011 golang.org/x/text v0.3.7
1112 gopkg.in/yaml.v2 v2.4.0
1213)
1314
14- require golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 // indirect
15+ require golang.org/x/sys v0.0.0-20211019181941-9d821ace8654 // indirect
Original file line number Diff line number Diff line change @@ -2,12 +2,14 @@ github.com/devlights/gomy v0.4.6 h1:S/UiHgNF1/dF/MRhxgOT1Bsd5i33/Ly9qOSePU3Q7KU=
22github.com/devlights/gomy v0.4.6 /go.mod h1:s6/L0jEn7J/F1bIRNu3nkf+Lz/n24RClTxD76DhqGEU =
33golang.org/x/crypto v0.0.0-20220214200702-86341886e292 h1:f+lwQ+GtmgoY+A2YaQxlSOnDjXcQ7ZRLWOHbC6HtRqE =
44golang.org/x/crypto v0.0.0-20220214200702-86341886e292 /go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4 =
5+ golang.org/x/exp v0.0.0-20220518171630-0b5c67f07fdf h1:oXVg4h2qJDd9htKxb5SCpFBHLipW6hXmL3qpUixS2jw =
6+ golang.org/x/exp v0.0.0-20220518171630-0b5c67f07fdf /go.mod h1:yh0Ynu2b5ZUe3MQfp2nM0ecK7wsgouWTDN0FNeJuIys =
57golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ =
68golang.org/x/sync v0.0.0-20210220032951-036812b2e83c /go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM =
7- golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 h1:SrN+KX8Art/Sf4HNj6Zcz06G7VEz+7w9tdXTPOZ7+l4 =
8- golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 /go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg =
9- golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 h1:JGgROgKl9N8DuW20oFS5gxc+lE67/N3FcwmBPMe7ArY =
10- golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 /go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8 =
9+ golang.org/x/sys v0.0.0-20211019181941-9d821ace8654 h1:id054HUawV2/6IGm2IV8KZQjqtwAOo2CYlOToYqa0d0 =
10+ golang.org/x/sys v0.0.0-20211019181941-9d821ace8654 /go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg =
11+ golang.org/x/term v0.0.0-20220411215600-e5f449aeb171 h1:EH1Deb8WZJ0xc0WK//leUHXcX9aLE5SymusoTmMZye8 =
12+ golang.org/x/term v0.0.0-20220411215600-e5f449aeb171 /go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8 =
1113golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk =
1214golang.org/x/text v0.3.7 /go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ =
1315gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM =
You can’t perform that action at this time.
0 commit comments