...
Source file
src/sync/example_test.go
Documentation: sync
1
2
3
4
5 package sync_test
6
7 import (
8 "fmt"
9 "sync"
10 )
11
12 type httpPkg struct{}
13
14 func (httpPkg) Get(url string) {}
15
16 var http httpPkg
17
18
19
20 func ExampleWaitGroup() {
21 var wg sync.WaitGroup
22 var urls = []string{
23 "http://www.golang.org/",
24 "http://www.google.com/",
25 "http://www.somestupidname.com/",
26 }
27 for _, url := range urls {
28
29 wg.Add(1)
30
31 go func(url string) {
32
33 defer wg.Done()
34
35 http.Get(url)
36 }(url)
37 }
38
39 wg.Wait()
40 }
41
42 func ExampleOnce() {
43 var once sync.Once
44 onceBody := func() {
45 fmt.Println("Only once")
46 }
47 done := make(chan bool)
48 for i := 0; i < 10; i++ {
49 go func() {
50 once.Do(onceBody)
51 done <- true
52 }()
53 }
54 for i := 0; i < 10; i++ {
55 <-done
56 }
57
58
59 }
60
View as plain text