1
0
mirror of https://github.com/golang/go synced 2024-09-25 01:20:13 -06:00

sync: add Once example

R=golang-dev, rsc, bradfitz
CC=golang-dev
https://golang.org/cl/5715046
This commit is contained in:
Dmitriy Vyukov 2012-03-01 22:16:20 +04:00
parent 986df83e0d
commit 2295554db6

View File

@ -5,6 +5,7 @@
package sync_test
import (
"fmt"
"net/http"
"sync"
)
@ -32,3 +33,22 @@ func ExampleWaitGroup() {
// Wait for all HTTP fetches to complete.
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
}