blob: 770c3cf2fe46e2e910eaadf2c7392231dce7f7c6 [file] [log] [blame] [view]
Andrew Gerrand5bc444d2014-12-10 11:35:11 +11001(TODO: Add table of contents.)
2
3# Introduction
4
5When new programmers start using Go or when old Go programmers start using a new concept, there are some common mistakes that many of them make. Here is a non-exhaustive list of some frequent mistakes that show up on the mailing lists and in IRC.
6
Cristian Măgherușan-Stanciu @magheru_san05efe8e2015-04-12 15:34:34 +02007# Using goroutines on loop iterator variables
Andrew Gerrand5bc444d2014-12-10 11:35:11 +11008
Cristian Măgherușan-Stanciu @magheru_san05efe8e2015-04-12 15:34:34 +02009When iterating in Go, one might also be tempted to use goroutines to process data in parallel. For example, you might write the following code:
Matthias Lüdtke8c2cac42015-07-07 18:51:23 +020010```go
Cristian Măgherușan-Stanciu @magheru_san05efe8e2015-04-12 15:34:34 +020011for val := range values {
12 go val.my_method()
13}
14```
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110015
Cristian Măgherușan-Stanciu @magheru_san05efe8e2015-04-12 15:34:34 +020016or if you wanted to process values coming in from a channel in their own goroutines, you might write something like this, using a closure:
17
Matthias Lüdtke8c2cac42015-07-07 18:51:23 +020018```go
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110019for val := range values {
Dave Day0d6986a2014-12-10 15:02:18 +110020 go func() {
21 fmt.Println(val)
22 }()
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110023}
24```
25
Cristian Măgherușan-Stanciu @magheru_san05efe8e2015-04-12 15:34:34 +020026
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110027If you don't immediately see the problem with the above code, take a second to see if you can figure out what is wrong with it before you keep reading.
28
Cristian Măgherușan-Stanciu @magheru_san05efe8e2015-04-12 15:34:34 +020029The ` val ` variable in the above loops is actually a single variable that takes on the value of each slice element. Because the closures are all only bound to that one variable, there is a very good chance that when you run this code you will see the last element printed for every iteration instead of each value in sequence, because the goroutines will probably not begin executing until after the loop.
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110030
Cristian Măgherușan-Stanciu @magheru_san05efe8e2015-04-12 15:34:34 +020031The proper way to write that closure loop is:
Matthias Lüdtke8c2cac42015-07-07 18:51:23 +020032```go
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110033for val := range values {
Dave Day0d6986a2014-12-10 15:02:18 +110034 go func(val interface{}) {
35 fmt.Println(val)
36 }(val)
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110037}
38```
39
40By adding val as a parameter to the closure, ` val ` is evaluated at each iteration and placed on the stack for the goroutine, so each slice element is available to the goroutine when it is eventually executed.
41
42It is also important to note that variables declared within the body of a loop are not shared between iterations, and thus can be used separately in a closure. The following code uses a common index variable ` i ` to create separate ` val `s, which results in the expected behavior:
43
Matthias Lüdtke8c2cac42015-07-07 18:51:23 +020044```go
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110045for i := range valslice {
Dave Day0d6986a2014-12-10 15:02:18 +110046 val := valslice[i]
47 go func() {
48 fmt.Println(val)
49 }()
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110050}
51```
52
53Note that without executing this closure as a goroutine, the code runs as expected. The following example prints out the integers between 1 and 10.
54
Matthias Lüdtke8c2cac42015-07-07 18:51:23 +020055```go
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110056for i := 1; i <= 10; i++ {
Dave Day0d6986a2014-12-10 15:02:18 +110057 func() {
58 fmt.Println(i)
59 }()
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110060}
61```
62
63Even though the closures all still close over the same variable (in this case, ` i `), they are executed before the variable changes, resulting in the desired behavior.
64
Cristian Măgherușan-Stanciu @magheru_san05efe8e2015-04-12 15:34:34 +020065http://golang.org/doc/go_faq.html#closures_and_goroutines