From bbb709be318b07d7e5cea70378b3ab35997c5a71 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Sat, 1 Dec 2012 08:08:31 -0500 Subject: [PATCH] Accepting fib count from os.Args, whoopdittydooo --- .../gotour-artifacts/selects/main.go | 21 ++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/gotime/src/gotime.example.org/gotour-artifacts/selects/main.go b/gotime/src/gotime.example.org/gotour-artifacts/selects/main.go index 1445318..706f0bf 100644 --- a/gotime/src/gotime.example.org/gotour-artifacts/selects/main.go +++ b/gotime/src/gotime.example.org/gotour-artifacts/selects/main.go @@ -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) }