Skip to content

Commit 3dea505

Browse files
authored
Merge pull request #874 from devlights/add-go123-iter-examples
2 parents 2dee3df + 270107e commit 3dea505

File tree

5 files changed

+92
-0
lines changed

5 files changed

+92
-0
lines changed

examples/basic/examples.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
"github.com/devlights/try-golang/examples/basic/interfaces"
3434
"github.com/devlights/try-golang/examples/basic/internalpkg"
3535
"github.com/devlights/try-golang/examples/basic/ioop"
36+
"github.com/devlights/try-golang/examples/basic/iters"
3637
"github.com/devlights/try-golang/examples/basic/jsonop"
3738
"github.com/devlights/try-golang/examples/basic/literals"
3839
"github.com/devlights/try-golang/examples/basic/logging"
@@ -122,6 +123,7 @@ func (r *register) Regist(m mapping.ExampleMapping) {
122123
imports.NewRegister().Regist(m)
123124
internalpkg.NewRegister().Regist(m)
124125
ioop.NewRegister().Regist(m)
126+
iters.NewRegister().Regist(m)
125127
jsonop.NewRegister().Regist(m)
126128
osop.NewRegister().Regist(m)
127129
profiles.NewRegister().Regist(m)

examples/basic/iters/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# サンプルリスト
2+
3+
このディレクトリには以下のサンプルがあります。
4+
5+
| file | example name | note |
6+
| ------------------- | ----------------------- | ----------------------------------------------------------- |
7+
| range_over_func1.go | iters_range_over_func_1 | Go 1.23 で正式導入となった Range-Over-Func のサンプルです。 |
8+
| | | |

examples/basic/iters/doc.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/*
2+
Package iters -- Go 1.23 で追加された iterパッケージとRange-Over-Func機能についてサンプルが配置されています.
3+
*/
4+
package iters

examples/basic/iters/examples.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package iters
2+
3+
import (
4+
"github.com/devlights/try-golang/mapping"
5+
)
6+
7+
type (
8+
register struct{}
9+
)
10+
11+
// NewRegister -- このパッケージ用のサンプルを登録する mapping.Register を生成します。
12+
func NewRegister() mapping.Register {
13+
return new(register)
14+
}
15+
16+
// Regist -- 登録します.
17+
func (r *register) Regist(m mapping.ExampleMapping) {
18+
m["iters_range_over_func_1"] = Go123RangeOverFunc1
19+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package iters
2+
3+
import "github.com/devlights/gomy/output"
4+
5+
// Go123RangeOverFunc1 は、Go 1.23 で正式導入となった Range-Over-Func のサンプルです。
6+
//
7+
// Range-Over-Func は、独自のイテレータを作成出来るようになる機能です。
8+
// 以下の関数パターンがサポートされています。
9+
//
10+
// - func(func() bool) : ループ変数に値を渡さないタイプ
11+
// - func(func(k) bool) : 1つのループ変数に値を渡すタイプ
12+
// - func(func(k, v) bool) : 2つのループ変数に値を渡すタイプ
13+
//
14+
// 本サンプルは、func(func() bool) (ループ変数に値を渡さないタイプ) についてのサンプルです。
15+
//
16+
// # REFERENCES
17+
// - https://tip.golang.org/doc/go1.23
18+
// - https://tip.golang.org/blog/range-functions
19+
// - https://tip.golang.org/ref/spec#For_range
20+
// - https://pkg.go.dev/iter@go1.23.3
21+
// - https://zenn.dev/koya_iwamura/articles/7e7482c7222e37
22+
// - https://tech.every.tv/entry/2023/12/09/1
23+
// - https://future-architect.github.io/articles/20240129a/
24+
func Go123RangeOverFunc1() error {
25+
var (
26+
// 2回分のイテレータ。ループ変数に値を渡さないタイプ。
27+
twoTimes = func(yield func() bool) {
28+
if !yield() {
29+
return
30+
}
31+
32+
if !yield() {
33+
return
34+
}
35+
}
36+
// クロージャを使って、指定された回数ループするイテレータを返す。
37+
nTimes = func(times int) func(func() bool) {
38+
return func(yield func() bool) {
39+
for range times {
40+
if !yield() {
41+
return
42+
}
43+
}
44+
}
45+
}
46+
)
47+
48+
// func() bool のイテレータなので、ループ毎に受け取るものが無い。
49+
// (range over twoTimes (variable of type func(yield func() bool)) permits no iteration variables)
50+
for range twoTimes {
51+
output.Stdoutl("[twoTimes ]", "call")
52+
}
53+
54+
for range nTimes(5) {
55+
output.Stdoutl("[nTimes(5)]", "call")
56+
}
57+
58+
return nil
59+
}

0 commit comments

Comments
 (0)