fizzBuzz
Now that we have looked at the different parts of the function, let's see how these parts work with various examples. Let's start with a classical programming game called fizzBuzz
. The rules of fizzBuzz
are straightforward. The fizzBuzz
function prints out various messages based on some math results. The rules perform one of the actions based on the number given:
- If the number is divisible by
3
, printFizz
. - If the number is divisible by
5
, printBuzz
. - If the number is divisible by
15
, printFizzBuzz
. - Else, print the number.
The following is the code snippet to achieve this output:
func fizzBuzz() { for i := 1; i <= 30; i++ { if i%15 == 0 { fmt.Println("FizzBuzz") } else if i%3 == 0 { ...