Created
July 1, 2014 19:15
-
-
Save jonbodner/467f761dc5d7894b33b8 to your computer and use it in GitHub Desktop.
Very silly 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" | |
type data struct { | |
i int | |
s string | |
} | |
func (d data) String() string { | |
if len(d.s) != 0 { | |
return d.s | |
} | |
return fmt.Sprintf("%d",d.i) | |
} | |
func test(i int, s string, inChan <-chan data) chan data { | |
outChan := make(chan data) | |
go func() { | |
for { | |
v := <- inChan | |
if v.i%i == 0 { | |
v.s += s | |
} | |
outChan <- v | |
} | |
}() | |
return outChan | |
} | |
func main() { | |
start := make(chan data) | |
three := test(3, "Fizz", start) | |
five := test(5, "Buzz", three) | |
for i := 1; i<=100;i++ { | |
start <- data{i:i} | |
result := <- five | |
fmt.Println(result) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment