Skip to content

Commit e12c0f3

Browse files
committed
Add exp_slices.go
1 parent e7917f5 commit e12c0f3

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-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_slices"
56
"github.com/devlights/try-golang/examples/generics/typeconstraints"
67
"github.com/devlights/try-golang/examples/generics/typeparameters"
78
"github.com/devlights/try-golang/mapping"
@@ -22,4 +23,5 @@ func (r *register) Regist(m mapping.ExampleMapping) {
2223
typeparameters.NewRegister().Regist(m)
2324
typeconstraints.NewRegister().Regist(m)
2425
exp_constraints.NewRegister().Regist(m)
26+
exp_slices.NewRegister().Regist(m)
2527
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package exp_slices
2+
3+
import (
4+
"github.com/devlights/gomy/output"
5+
"golang.org/x/exp/slices"
6+
)
7+
8+
func ExpSlices() error {
9+
var (
10+
s1 = []string{"hello", "world"}
11+
s2 = []string{"hello", "world"}
12+
s3 = []string{"world", "hello"}
13+
s4 = []int{100, 101}
14+
)
15+
16+
output.Stdoutl("[Equal(s1, s2)]", slices.Equal(s1, s2))
17+
output.Stdoutl("[Equal(s2, s3)]", slices.Equal(s2, s3))
18+
// compile error
19+
//fmt.Println(slices.Equal(s1, s4))
20+
21+
s5 := slices.Insert(s4, 1, 999)
22+
output.Stdoutl("[Insert]", s4, s5)
23+
24+
idx := slices.Index(s5, 999)
25+
s6 := slices.Delete(s5, idx, idx+1)
26+
output.Stdoutl("[Delete]", s5, s6)
27+
28+
return nil
29+
}

0 commit comments

Comments
 (0)