Accepting fib count from os.Args, whoopdittydooo

This commit is contained in:
Dan Buch 2012-12-01 08:08:31 -05:00
parent b01f9d70fd
commit bbb709be31

View File

@ -1,6 +1,10 @@
package main
import "fmt"
import (
"fmt"
"os"
"strconv"
)
func fibonacci(c, quit chan int) {
x, y := 0, 1
@ -9,20 +13,27 @@ func fibonacci(c, quit chan int) {
case c <- x:
x, y = y, x + y
case <- quit:
fmt.Println("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() {
for i := 0; i < 10; i++ {
go func(count int) {
for i := 0; i < count; i++ {
fmt.Println(<-c)
}
quit <- 0
}()
}(count)
fibonacci(c, quit)
}