Skip to content

Instantly share code, notes, and snippets.

@laclefyoshi
Created January 29, 2012 12:55
Show Gist options
  • Save laclefyoshi/1698701 to your computer and use it in GitHub Desktop.
Save laclefyoshi/1698701 to your computer and use it in GitHub Desktop.
fizzbuzz in go
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