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 { Â Â Â Â Â Â Â Â ...