Created
August 20, 2016 14:35
-
-
Save klrkdekira/680545cafff3018d4daa6e843dcd27c9 to your computer and use it in GitHub Desktop.
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() { | |
routineCount := 100 | |
// bootstrapping the waitgroup | |
var wg sync.WaitGroup | |
// increment the routine count | |
wg.Add(routineCount) | |
for i := 1; i <= routineCount; i++ { | |
// it is possible to do it here as long as wg is increment before goroutine | |
// wg.Add(1) | |
// gotcha of closure with goroutine | |
go func(i int) { | |
fizzbuzz(i) | |
wg.Done() | |
}(i) | |
} | |
// Block until all executed | |
wg.Wait() | |
} | |
func fizzbuzz(i int) { | |
// 0 padding for prettier output | |
msg := fmt.Sprintf("%03d - ", i) | |
if i%3 == 0 { | |
msg += "fizz" | |
} | |
if i%5 == 0 { | |
msg += "buzz" | |
} | |
fmt.Println(msg) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment