Skip to content

Instantly share code, notes, and snippets.

@EricR
Last active December 15, 2015 09:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EricR/5239806 to your computer and use it in GitHub Desktop.
Save EricR/5239806 to your computer and use it in GitHub Desktop.
FizzBuzz using `switch`, as per exercise of "The Way to Go".
// fizzbuzz
// A program that counts from 1 to 100, replacing numbers divisible by 3 with "Fizz",
// those divisible by 5 with "Buzz", and those divisble by both 3 and 5 with "FizzBuzz".
package main
import "fmt"
import "strconv"
func main() {
for i := 1; i <= 100; i++ {
fmt.Println(fizzBuzz(i))
}
}
func fizzBuzz(i int) (s string) {
switch {
case i%5 == 0 && i%3 == 0:
return "FizzBuzz"
case i%3 == 0:
return "Fizz"
case i%5 == 0:
return "Buzz"
}
return strconv.Itoa(i)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment