Fizz-Buzz Test in Go

· 135 words · 1 minute read

The fizzbuzz test is a simple program, often used in interviews to identify people who stuggle to code.

The program should print the numbers from 1 to 100, except if the number is divisible by 3 then print ‘fizz’, if the number is divisible by 5 print ‘buzz’ or if the number if divisible by both print ‘fizzbuzz’.

package main

import "fmt"

func main() {
    for i := 1; i <= 100; i++ {

        if i%3 == 0 {
            // Multiple of 3
            fmt.Printf("fizz")
        }
        if i%5 == 0 {
            // Multiple of 5
            fmt.Printf("buzz")
        }

        if i%3 != 0 && i%5 != 0 {
            // Neither, so print the number itself
            fmt.Printf("%d", i)
        }

        // A trailing new line (so both fizz + buzz can be printed on the same line)
        fmt.Printf("\n")

    }
}
Image of Author Edd Turtle

Author:  Edd Turtle

Edd is the Lead Developer at Hoowla, a prop-tech startup, where he spends much of his time working on production-ready Go and PHP code. He loves coding, but also enjoys cycling and camping in his spare time.

See something which isn't right? You can contribute to this page on GitHub or just let us know in the comments below - Thanks for reading!