The problem

Write a program to print numbers from 1 to 100. Print “Fizz” for the number 3 and “Buzz” for the number 5. When numbers are multiples of three and five at the same time, print “FizzBuzz”.

Train of thought

If the result is yes, print “Fizz” and set TMP (bool) to true. If the result is yes, print “Buzz” and set TMP to true. If TMP is true, the value is printed. Otherwise, the value is not printed.

package main

import "fmt"

func main(a) {
	const (
		FIZZ = 3
		BUZZ = 5
	)
	var p bool
	for i := 1; i < 100; i++ {
		p = false
		if i%FIZZ == 0 {
			fmt.Printf("Fizz")
			p = true
		}
		if i%BUZZ == 0 {
			fmt.Printf("Buzz")
			p = true
		}
		if! p { fmt.Printf("%v", i)
		}
		fmt.Println()
	}
}

Copy the code

The results

Here are the top 20 running results

1
2       
Fizz    
4       
Buzz    
Fizz    
7       
8       
Fizz    
Buzz    
11      
Fizz    
13      
14      
FizzBuzz
16      
17      
Fizz    
19      
Buzz  
Copy the code

Source of problem

Learning GO. PDF version