You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

40 lines
701 B

package main
import (
"fmt"
"os"
"strconv"
)
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() {
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)
}
}
c := make(chan int)
quit := make(chan int)
go func(count int) {
for i := 0; i < count; i++ {
fmt.Println(<-c)
}
quit <- 0
}(count)
fibonacci(c, quit)
}