Created
April 24, 2019 14:06
-
-
Save tomcha/12cb68e98e49fefb4d1ef26809c4b4a3 to your computer and use it in GitHub Desktop.
fizz buzz + goroutine
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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