]> Cypherpunks repositories - gostls13.git/commitdiff
sync: add Once example
authorDmitriy Vyukov <dvyukov@google.com>
Thu, 1 Mar 2012 18:16:20 +0000 (22:16 +0400)
committerDmitriy Vyukov <dvyukov@google.com>
Thu, 1 Mar 2012 18:16:20 +0000 (22:16 +0400)
R=golang-dev, rsc, bradfitz
CC=golang-dev
https://golang.org/cl/5715046

src/pkg/sync/example_test.go

index 1424b1e79e66021dac04c6779161b6744b0e4f68..15649240035718d5130a7ee5aecf8028058e5d03 100644 (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
+}