Skip to content

Instantly share code, notes, and snippets.

@klrkdekira
Created August 20, 2016 14:35
Show Gist options
  • Save klrkdekira/680545cafff3018d4daa6e843dcd27c9 to your computer and use it in GitHub Desktop.
Save klrkdekira/680545cafff3018d4daa6e843dcd27c9 to your computer and use it in GitHub Desktop.
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