Created
January 22, 2014 08:41
-
-
Save alwinmark/8555414 to your computer and use it in GitHub Desktop.
Fizzbuzz Example with channels. So its possible to take as many Fizzbuzz items as you want. [1] infos about go and channels: https://docs.google.com/presentation/d/1IzRQAZ5oWhAaD62WNP7QT44b7QAf0S6uBPTtNXz9c6A/edit?pli=1#slide=id.g62e28700_0_6
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" | |
"strconv" | |
) | |
func Fizzbuzz(in <-chan int, out chan<- string) { | |
for { | |
x := <-in | |
if x % 15 == 0 { | |
out <- "fizzbuzz" | |
} else if x % 3 == 0 { | |
out <- "fizz" | |
} else if x % 5 == 0 { | |
out <- "buzz" | |
} else { | |
out <- strconv.Itoa(x) | |
} | |
} | |
} | |
func main() { | |
ch := make(chan int, 5) | |
fizzbuzz := make(chan string, 5) | |
go func () { | |
for i := 1;; i++ { | |
ch <- i | |
} | |
}() | |
go Fizzbuzz(ch, fizzbuzz) | |
for i := 0; i < 20; i++ { | |
fmt.Printf("%s\n", <-fizzbuzz) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment