Skip to content

Instantly share code, notes, and snippets.

@udhos
Created May 11, 2018 00:39
Show Gist options
  • Save udhos/51d30efccb51468918a47fd72b3a9e75 to your computer and use it in GitHub Desktop.
Save udhos/51d30efccb51468918a47fd72b3a9e75 to your computer and use it in GitHub Desktop.
sync.Once
// https://golang.org/pkg/sync/#Once
package main
import (
"fmt"
"sync"
)
func main() {
var once sync.Once
onceBody := func() {
fmt.Println("Only once")
}
done := make(chan bool)
for i := 0; i < 10; i++ {
go func() {
once.Do(onceBody)
done <- true
}()
}
for i := 0; i < 10; i++ {
<-done
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment