File tree Expand file tree Collapse file tree 3 files changed +59
-3
lines changed Expand file tree Collapse file tree 3 files changed +59
-3
lines changed Original file line number Diff line number Diff line change
1
+ package runtime
2
+
3
+ // A naive implementation of coroutines that supports
4
+ // package iter.
5
+
6
+ type coro struct {
7
+ f func (* coro )
8
+ ch chan struct {}
9
+ }
10
+
11
+ //go:linkname newcoro
12
+
13
+ func newcoro (f func (* coro )) * coro {
14
+ c := & coro {
15
+ ch : make (chan struct {}),
16
+ f : f ,
17
+ }
18
+ go func () {
19
+ defer close (c .ch )
20
+ <- c .ch
21
+ f (c )
22
+ }()
23
+ return c
24
+ }
25
+
26
+ //go:linkname coroswitch
27
+
28
+ func coroswitch (c * coro ) {
29
+ c .ch <- struct {}{}
30
+ <- c .ch
31
+ }
Original file line number Diff line number Diff line change 1
1
package main
2
2
3
+ import "iter"
4
+
3
5
func main () {
4
6
testFuncRange (counter )
7
+ testIterPull (counter )
8
+ println ("go1.23 has lift-off!" )
5
9
}
6
10
7
- func testFuncRange (f func (yield func (int ) bool )) {
8
- for i := range f {
11
+ func testFuncRange (it iter.Seq [int ]) {
12
+ for i := range it {
13
+ println (i )
14
+ }
15
+ }
16
+
17
+ func testIterPull (it iter.Seq [int ]) {
18
+ next , stop := iter .Pull (it )
19
+ defer stop ()
20
+ for {
21
+ i , ok := next ()
22
+ if ! ok {
23
+ break
24
+ }
9
25
println (i )
10
26
}
11
- println ("go1.23 has lift-off!" )
12
27
}
13
28
14
29
func counter (yield func (int ) bool ) {
Original file line number Diff line number Diff line change 8
8
3
9
9
2
10
10
1
11
+ 10
12
+ 9
13
+ 8
14
+ 7
15
+ 6
16
+ 5
17
+ 4
18
+ 3
19
+ 2
20
+ 1
11
21
go1.23 has lift-off!
You can’t perform that action at this time.
0 commit comments