Commit 2295554d authored by Dmitriy Vyukov's avatar Dmitriy Vyukov

sync: add Once example

R=golang-dev, rsc, bradfitz
CC=golang-dev
https://golang.org/cl/5715046
parent 986df83e
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
package sync_test package sync_test
import ( import (
"fmt"
"net/http" "net/http"
"sync" "sync"
) )
...@@ -32,3 +33,22 @@ func ExampleWaitGroup() { ...@@ -32,3 +33,22 @@ func ExampleWaitGroup() {
// Wait for all HTTP fetches to complete. // Wait for all HTTP fetches to complete.
wg.Wait() wg.Wait()
} }
func ExampleOnce() {
var once sync.Once
onceBody := func() {
fmt.Printf("Only once\n")
}
done := make(chan bool)
for i := 0; i < 10; i++ {
go func() {
once.Do(onceBody)
done <- true
}()
}
for i := 0; i < 10; i++ {
<-done
}
// Output:
// Only once
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment