Skip to content

Instantly share code, notes, and snippets.

@mnrtks
Created October 20, 2013 09:47
Show Gist options
  • Save mnrtks/7067245 to your computer and use it in GitHub Desktop.
Save mnrtks/7067245 to your computer and use it in GitHub Desktop.
FizzBuzz written by go with channel.
package main
import (
"fmt"
"strconv"
)
func main() {
for i := range fizzbuzz(100) {
fmt.Println(i)
}
}
func fizzbuzz(n int) chan string {
ch := make(chan string)
go func() {
for i := 0; i < n; i++ {
if i % 15 == 0 {
ch <- "FizzBuzz"
} else if i % 5 == 0 {
ch <- "Buzz"
} else if i % 3 == 0 {
ch <- "Fizz"
} else {
ch <- strconv.Itoa(i)
}
}
close(ch)
}()
return ch
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment