runtime: Select/TrySelect

This commit is contained in:
xushiwei
2024-07-06 21:59:28 +08:00
parent b0941faf88
commit 453faa6a76
8 changed files with 297 additions and 28 deletions

26
_demo/chansel/chansel.go Normal file
View File

@@ -0,0 +1,26 @@
package main
func fibonacci(c, quit chan int) {
x, y := 0, 1
for {
select {
case c <- x:
x, y = y, x+y
case <-quit:
println("quit")
return
}
}
}
func main() {
c := make(chan int)
quit := make(chan int)
go func() {
for i := 0; i < 10; i++ {
println(<-c)
}
close(quit)
}()
fibonacci(c, quit)
}