I have been thinking of learning golang for sometime now and decided that well an exercise such as fizzbuzz would be a great way to start familiarizing myself with the language.
What is FizzBuzz?
Fizz buzz is a group word game for children to teach them about division. Players take turns to count incrementally, replacing any number divisible by three with the word fizz, and any number divisible by five with the word buzz. — Wikipedia
For Example, in a range 1-15, it will go like:
|
|
Conventionally the range being 1-100.
The Problem
Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”.
My Solution
It is easy to build a simple fizzbuzz solution, doing a loop to 100 and hard coding divisibility checks like if i%3 == 0
, but I want to generalize my code as much as possible. That is, make changing the inputs very easy without affecting the logic of the program.
Here is my solution:
|
|
Observations
-
Using a struct instead of a map. I should have probably used a map instead, but really wanted to do this with a struct. I will say, it turned out better than I could have hoped for. The more I look at it, the more I think It should have used a map, but I will resist, this is my masterpiece.
-
On line 27, the for loop uses
_
because and index value is not needed. Interesting design choice. -
else
start the same line as I close the bracket for the if statement. I cannot place the else statement on the next line, that is a syntax error. If I think about it, it does make sense that the related else statement should be nested, but it does feel weird doing that, since i have a habit of placing the else statement on the next line. The 2 minutes I did not understand why this was happening, I felt very dumb. -
There is no output. Wonder what happened to it, you are now gonna have to run it on your own to find out, if the output is correct or not.
Closing words
It was pretty fun writing this thing in golang. The documentation was full of great examples which made understanding the basics really easy. Probably one of the easiest languages to learn, if you know any other programming language.
I would say it is an above average implementation of fizzbuzz. It may not be the fastest, or the most generalized one, but this much is sure, if you look at it, it surely is an okay fizzbuzz implementation.
I do think there is still improvement that can be to this. I may comeback to this on a later date once I am more comfortable in the language.