2. Logic and Loops
Activity 2.01: Implementing FizzBuzz
Solution:
- Define
package
and includeimport
:package main import ( Â Â "fmt" Â Â "strconv" )
- Create the
main
function:func main() {
- Create a
for
loop that starts at 1 and loops untili
gets to 100:Â Â for i := 1; i <= 100; i++{
- Initialize a string variable that will hold the output:
  out := ""
- Using module logic to check for divisibility,
if
i
is divisible by 3, then add"Fizz"
to theout
string:Â Â if i%3 == 0 { Â Â out += "Fizz" Â Â }
- If divisible by 5, add
"Buzz"
to the string:Â Â if i%5 == 0 { Â Â out += "Buzz" Â Â }
- If neither, convert the number to a string and then add it to the output string:
  if out == "" {   out = strconv.Itoa(i)   }
- Print the output variable:
  fmt.Println(out)
...