mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-08-12 07:34:06 +08:00
43 lines
458 B
Go
Executable File
43 lines
458 B
Go
Executable File
// gofibonacci_select.go
|
|
package main
|
|
|
|
import "fmt"
|
|
|
|
func fibonacci(c, quit chan int) {
|
|
x, y := 1, 1
|
|
for {
|
|
select {
|
|
case c <- x:
|
|
x, y = y, x+y
|
|
case <-quit:
|
|
fmt.Println("quit")
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
func main() {
|
|
c := make(chan int)
|
|
quit := make(chan int)
|
|
go func() {
|
|
for i := 0; i < 10; i++ {
|
|
fmt.Println(<-c)
|
|
}
|
|
quit <- 0
|
|
}()
|
|
fibonacci(c, quit)
|
|
}
|
|
/* Output:
|
|
1
|
|
1
|
|
2
|
|
3
|
|
5
|
|
8
|
|
13
|
|
21
|
|
34
|
|
55
|
|
quit
|
|
*/
|