]> Cypherpunks repositories - gostls13.git/commitdiff
sync: improve WaitGroup example by putting the call to Done in a
authorGaal Yahas <gaal@google.com>
Wed, 6 Feb 2013 16:39:52 +0000 (00:39 +0800)
committerShenghou Ma <minux.ma@gmail.com>
Wed, 6 Feb 2013 16:39:52 +0000 (00:39 +0800)
deferred block. This makes hangs in the waiting code less likely
if a goroutine exits abnormally.

R=golang-dev, minux.ma
CC=golang-dev
https://golang.org/cl/7306052

src/pkg/sync/example_test.go

index 15649240035718d5130a7ee5aecf8028058e5d03..031c87f03b157785dc7d949a63db2e7b9a6dcb9d 100644 (file)
@@ -24,10 +24,10 @@ func ExampleWaitGroup() {
                wg.Add(1)
                // Launch a goroutine to fetch the URL.
                go func(url string) {
+                       // Decrement the counter when the goroutine completes.
+                       defer wg.Done()
                        // Fetch the URL.
                        http.Get(url)
-                       // Decrement the counter.
-                       wg.Done()
                }(url)
        }
        // Wait for all HTTP fetches to complete.
@@ -37,7 +37,7 @@ func ExampleWaitGroup() {
 func ExampleOnce() {
        var once sync.Once
        onceBody := func() {
-               fmt.Printf("Only once\n")
+               fmt.Println("Only once")
        }
        done := make(chan bool)
        for i := 0; i < 10; i++ {