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