Skip to content

Instantly share code, notes, and snippets.

@alvaroloes
Created April 4, 2019 10:55
Show Gist options
  • Save alvaroloes/fa1da50098eeab033f69e94200cc7041 to your computer and use it in GitHub Desktop.
Save alvaroloes/fa1da50098eeab033f69e94200cc7041 to your computer and use it in GitHub Desktop.
FizzBuzz without conditionals in Go
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