Skip to content

Instantly share code, notes, and snippets.

@tomcha
Created Apr 24, 2019
Embed
What would you like to do?
fizz buzz + goroutine
package main
import (
"fmt"
"sync"
)
func main() {
var wg sync.WaitGroup
wg.Add(1)
go OneHundled(&wg)
TwoHundled()
wg.Wait()
}
func Fizz(i int) string {
if i%3 == 0 {
return "Fizz"
} else {
return ""
}
}
func Buzz(i int) string {
if i%5 == 0 {
return "Buzz"
} else {
return ""
}
}
func OneHundled(wg *sync.WaitGroup) {
var str string
for i := 1; i <= 100; i++ {
str = Fizz(i) + Buzz(i)
if str == "" {
fmt.Println(i)
} else {
fmt.Printf(str+" %d\n", i)
}
}
wg.Done()
}
func TwoHundled() {
var str string
for i := 101; i <= 200; i++ {
str = Fizz(i) + Buzz(i)
if str == "" {
fmt.Println(i)
} else {
fmt.Printf(str+" %d\n", i)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment