Created
April 4, 2019 10:55
-
-
Save alvaroloes/fa1da50098eeab033f69e94200cc7041 to your computer and use it in GitHub Desktop.
FizzBuzz without conditionals 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 mods struct{ mod3, mod5 bool } | |
var m = map[mods]string{ | |
{false, false}: "", | |
{true, false}: "Fizz", | |
{false, true}: "Buzz", | |
{true, true}: "FizzBuzz", | |
} | |
func fizzbuzz(n int) string { | |
return m[mods{n%3 == 0, n%5 == 0}] | |
} | |
func main() { | |
for i := 1; i <= 100; i++ { | |
fmt.Println(i, fizzbuzz(i)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment