2012-12-01 12:52:03 +00:00
|
|
|
package main
|
|
|
|
|
2012-12-01 13:08:31 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
)
|
2012-12-01 12:52:03 +00:00
|
|
|
|
|
|
|
func fibonacci(c, quit chan int) {
|
|
|
|
x, y := 0, 1
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case c <- x:
|
|
|
|
x, y = y, x + y
|
|
|
|
case <- quit:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2012-12-01 13:08:31 +00:00
|
|
|
count := 10
|
|
|
|
if len(os.Args) > 1 {
|
|
|
|
var err error
|
|
|
|
if count, err = strconv.Atoi(os.Args[1]); err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, os.Args[1], "is not an int!")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
2012-12-01 12:52:03 +00:00
|
|
|
c := make(chan int)
|
|
|
|
quit := make(chan int)
|
2012-12-01 13:08:31 +00:00
|
|
|
go func(count int) {
|
|
|
|
for i := 0; i < count; i++ {
|
2012-12-01 12:52:03 +00:00
|
|
|
fmt.Println(<-c)
|
|
|
|
}
|
|
|
|
quit <- 0
|
2012-12-01 13:08:31 +00:00
|
|
|
}(count)
|
2012-12-01 12:52:03 +00:00
|
|
|
fibonacci(c, quit)
|
|
|
|
}
|