blob: f04ee3f01de606840abd74c5244eade4626e2fbb [file] [log] [blame] [view]
Ilya Sevostyanovc289dec2016-11-10 14:04:40 +03001Table of Contents
2=================
3
4+ [Introduction](#introduction)
5+ [Using goroutines on loop iterator variables](#using-goroutines-on-loop-iterator-variables)
Andrew Gerrand5bc444d2014-12-10 11:35:11 +11006
7# Introduction
8
9When 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.
10
Cristian Măgherușan-Stanciu @magheru_san05efe8e2015-04-12 15:34:34 +020011# Using goroutines on loop iterator variables
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110012
Eno Compton6cf536b2016-11-13 11:26:27 -070013When 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 +020014```go
Cristian Măgherușan-Stanciu @magheru_san05efe8e2015-04-12 15:34:34 +020015for val := range values {
Eno Compton6cf536b2016-11-13 11:26:27 -070016 go val.MyMethod()
Cristian Măgherușan-Stanciu @magheru_san05efe8e2015-04-12 15:34:34 +020017}
18```
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110019
Cristian Măgherușan-Stanciu @magheru_san05efe8e2015-04-12 15:34:34 +020020or if you wanted to process values coming in from a channel in their own goroutines, you might write something like this, using a closure:
21
Matthias Lüdtke8c2cac42015-07-07 18:51:23 +020022```go
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110023for val := range values {
Dave Day0d6986a2014-12-10 15:02:18 +110024 go func() {
25 fmt.Println(val)
26 }()
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110027}
28```
29
Cristian Măgherușan-Stanciu @magheru_san05efe8e2015-04-12 15:34:34 +020030
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110031If 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.
32
Cristian Măgherușan-Stanciu @magheru_san05efe8e2015-04-12 15:34:34 +020033The ` 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 +110034
Cristian Măgherușan-Stanciu @magheru_san05efe8e2015-04-12 15:34:34 +020035The proper way to write that closure loop is:
Matthias Lüdtke8c2cac42015-07-07 18:51:23 +020036```go
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110037for val := range values {
Dave Day0d6986a2014-12-10 15:02:18 +110038 go func(val interface{}) {
39 fmt.Println(val)
40 }(val)
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110041}
42```
43
44By 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.
45
46It 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:
47
Matthias Lüdtke8c2cac42015-07-07 18:51:23 +020048```go
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110049for i := range valslice {
Dave Day0d6986a2014-12-10 15:02:18 +110050 val := valslice[i]
51 go func() {
52 fmt.Println(val)
53 }()
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110054}
55```
56
57Note that without executing this closure as a goroutine, the code runs as expected. The following example prints out the integers between 1 and 10.
58
Matthias Lüdtke8c2cac42015-07-07 18:51:23 +020059```go
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110060for i := 1; i <= 10; i++ {
Dave Day0d6986a2014-12-10 15:02:18 +110061 func() {
62 fmt.Println(i)
63 }()
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110064}
65```
66
67Even 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.
68
oleksiif917f0862015-12-05 12:04:43 +070069http://golang.org/doc/go_faq.html#closures_and_goroutines