Created
January 29, 2012 12:55
-
-
Save laclefyoshi/1698701 to your computer and use it in GitHub Desktop.
fizzbuzz in go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"os" | |
"strconv" | |
) | |
func stream(ch chan int) { | |
for i := 1; ; i++ { | |
ch <- i | |
} | |
} | |
func fizzBuzz(num int) { | |
switch { | |
case num % 15 == 0: | |
fmt.Println("FizzBuzz") | |
case num % 3 == 0: | |
fmt.Println("Fizz") | |
case num % 5 == 0: | |
fmt.Println("Buzz") | |
default: | |
fmt.Println(num) | |
} | |
} | |
func main() { | |
ch := make(chan int) | |
go stream(ch) | |
if len(os.Args) > 1 { | |
max, err := strconv.Atoi(os.Args[1]) | |
if err != nil { | |
fmt.Println("invalid argument") | |
os.Exit(1) | |
} | |
for i := 0; i < max; i++ { | |
num := <- ch | |
fizzBuzz(num) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment