Created
October 20, 2013 09:47
-
-
Save mnrtks/7067245 to your computer and use it in GitHub Desktop.
FizzBuzz written by go with channel.
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 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