Modulo

Fizzbuzz

Go supports the standard modulo operatorarrow-up-right:

7 % 3 // 1

The AND logical operatorarrow-up-right:

true && false // false
true && true // true

As well as the OR operator:

true || false // true
false || false // false

Assignment

We're hiring engineers at Textio, so time to brush up on the classic "Fizzbuzz" game, a coding exercise that has been dramatically overused in coding interviews across the world.

Complete the fizzbuzz function that prints the numbers 1 to 100 inclusive each on their own line, but replace multiples of 3 with the text fizz and multiples of 5 with buzz. Print fizzbuzz for multiples of 3 AND 5.

This lesson is graded based on the output of your program, so don't leave any debugging print statements in your code.

Solution

Key Go Concepts Used

  • for loop (Go’s only looping construct)

  • Modulo operator %

  • Logical AND &&

  • Conditional branching (if / else if / else)

  • fmt.Println() for line-by-line output


Correct & Idiomatic Go Solution

Last updated