Text file
tour/concurrency.article
1 Concurrency
2 Go provides concurrency constructions as part of the core language. This lesson presents them and gives some examples on how they can be used.
3
4 The Go Authors
5 https://golang.org
6
7 * Goroutines
8
9 A _goroutine_ is a lightweight thread managed by the Go runtime.
10
11 go f(x, y, z)
12
13 starts a new goroutine running
14
15 f(x, y, z)
16
17 The evaluation of `f`, `x`, `y`, and `z` happens in the current goroutine and the execution of `f` happens in the new goroutine.
18
19 Goroutines run in the same address space, so access to shared memory must be synchronized. The [[/pkg/sync/][`sync`]] package provides useful primitives, although you won't need them much in Go as there are other primitives. (See the next slide.)
20
21 .play concurrency/goroutines.go
22
23 * Channels
24
25 Channels are a typed conduit through which you can send and receive values with the channel operator, `<-`.
26
27 ch <- v // Send v to channel ch.
28 v := <-ch // Receive from ch, and
29 // assign value to v.
30
31 (The data flows in the direction of the arrow.)
32
33 Like maps and slices, channels must be created before use:
34
35 ch := make(chan int)
36
37 By default, sends and receives block until the other side is ready. This allows goroutines to synchronize without explicit locks or condition variables.
38
39 The example code sums the numbers in a slice, distributing the work between two goroutines.
40 Once both goroutines have completed their computation, it calculates the final result.
41
42 .play concurrency/channels.go
43
44 * Buffered Channels
45
46 Channels can be _buffered_. Provide the buffer length as the second argument to `make` to initialize a buffered channel:
47
48 ch := make(chan int, 100)
49
50 Sends to a buffered channel block only when the buffer is full. Receives block when the buffer is empty.
51
52 Modify the example to overfill the buffer and see what happens.
53
54 .play concurrency/buffered-channels.go
55
56 * Range and Close
57
58 A sender can `close` a channel to indicate that no more values will be sent. Receivers can test whether a channel has been closed by assigning a second parameter to the receive expression: after
59
60 v, ok := <-ch
61
62 `ok` is `false` if there are no more values to receive and the channel is closed.
63
64 The loop `for`i`:=`range`c` receives values from the channel repeatedly until it is closed.
65
66 *Note:* Only the sender should close a channel, never the receiver. Sending on a closed channel will cause a panic.
67
68 *Another*note:* Channels aren't like files; you don't usually need to close them. Closing is only necessary when the receiver must be told there are no more values coming, such as to terminate a `range` loop.
69
70 .play concurrency/range-and-close.go
71
72 * Select
73
74 The `select` statement lets a goroutine wait on multiple communication operations.
75
76 A `select` blocks until one of its cases can run, then it executes that case. It chooses one at random if multiple are ready.
77
78 .play concurrency/select.go
79
80 * Default Selection
81
82 The `default` case in a `select` is run if no other case is ready.
83
84 Use a `default` case to try a send or receive without blocking:
85
86 select {
87 case i := <-c:
88 // use i
89 default:
90 // receiving from c would block
91 }
92
93 .play concurrency/default-selection.go
94
95 * Exercise: Equivalent Binary Trees
96
97 There can be many different binary trees with the same sequence of values stored in it. For example, here are two binary trees storing the sequence 1, 1, 2, 3, 5, 8, 13.
98
99 .image /tour/static/img/tree.png
100
101 A function to check whether two binary trees store the same sequence is quite complex in most languages. We'll use Go's concurrency and channels to write a simple solution.
102
103 This example uses the `tree` package, which defines the type:
104
105 type Tree struct {
106 Left *Tree
107 Value int
108 Right *Tree
109 }
110
111
112 Continue description on [[javascript:click('.next-page')][next page]].
113
114 * Exercise: Equivalent Binary Trees
115
116 *1.* Implement the `Walk` function.
117
118 *2.* Test the `Walk` function.
119
120 The function `tree.New(k)` constructs a randomly-structured (but always sorted) binary tree holding the values `k`, `2k`, `3k`, ..., `10k`.
121
122 Create a new channel `ch` and kick off the walker:
123
124 go Walk(tree.New(1), ch)
125
126 Then read and print 10 values from the channel. It should be the numbers 1, 2, 3, ..., 10.
127
128 *3.* Implement the `Same` function using `Walk` to determine whether `t1` and `t2` store the same values.
129
130 *4.* Test the `Same` function.
131
132 `Same(tree.New(1),`tree.New(1))` should return true, and `Same(tree.New(1),`tree.New(2))` should return false.
133
134 The documentation for `Tree` can be found [[https://godoc.org/golang.org/x/tour/tree#Tree][here]].
135
136 .play concurrency/exercise-equivalent-binary-trees.go
137
138 * sync.Mutex
139
140 We've seen how channels are great for communication among goroutines.
141
142 But what if we don't need communication? What if we just want to make sure only
143 one goroutine can access a variable at a time to avoid conflicts?
144
145 This concept is called _mutual_exclusion_, and the conventional name for the data structure that provides it is _mutex_.
146
147 Go's standard library provides mutual exclusion with
148 [[/pkg/sync/#Mutex][`sync.Mutex`]] and its two methods:
149
150 - `Lock`
151 - `Unlock`
152
153 We can define a block of code to be executed in mutual exclusion by surrounding it
154 with a call to `Lock` and `Unlock` as shown on the `Inc` method.
155
156 We can also use `defer` to ensure the mutex will be unlocked as in the `Value` method.
157
158 .play concurrency/mutex-counter.go
159
160 * Exercise: Web Crawler
161
162 In this exercise you'll use Go's concurrency features to parallelize a web crawler.
163
164 Modify the `Crawl` function to fetch URLs in parallel without fetching the same URL twice.
165
166 _Hint_: you can keep a cache of the URLs that have been fetched on a map, but maps alone are not
167 safe for concurrent use!
168
169 .play concurrency/exercise-web-crawler.go
170
171 * Where to Go from here...
172
173 #appengine: You can get started by
174 #appengine: [[/doc/install/][installing Go]].
175
176 #appengine: Once you have Go installed, the
177 The
178 [[/doc/][Go Documentation]] is a great place to
179 #appengine: continue.
180 start.
181 It contains references, tutorials, videos, and more.
182
183 To learn how to organize and work with Go code, read [[/doc/code][How to Write Go Code]].
184
185 If you need help with the standard library, see the [[/pkg/][package reference]]. For help with the language itself, you might be surprised to find the [[/ref/spec][Language Spec]] is quite readable.
186
187 To further explore Go's concurrency model, watch
188 [[https://www.youtube.com/watch?v=f6kdp27TYZs][Go Concurrency Patterns]]
189 ([[/talks/2012/concurrency.slide][slides]])
190 and
191 [[https://www.youtube.com/watch?v=QDDwwePbDtw][Advanced Go Concurrency Patterns]]
192 ([[/talks/2013/advconc.slide][slides]])
193 and read the
194 [[/doc/codewalk/sharemem/][Share Memory by Communicating]]
195 codewalk.
196
197 To get started writing web applications, watch
198 [[https://vimeo.com/53221558][A simple programming environment]]
199 ([[/talks/2012/simple.slide][slides]])
200 and read the
201 [[/doc/articles/wiki/][Writing Web Applications]] tutorial.
202
203 The [[/doc/codewalk/functions/][First Class Functions in Go]] codewalk gives an interesting perspective on Go's function types.
204
205 The [[/blog/][Go Blog]] has a large archive of informative Go articles.
206
207 Visit [[/][the Go home page]] for more.
208
209
View as plain text