mirror of
https://github.com/golang/go.git
synced 2025-05-05 23:53:05 +00:00
Updated CommonMistakes (markdown)
parent
f4f69696b9
commit
88d63a97c0
@ -12,7 +12,7 @@ When new programmers start using Go or when old Go programmers start using a new
|
|||||||
|
|
||||||
When iterating in Go, one might also be tempted to use goroutines to process data in parallel. For example, you might write the following code:
|
When iterating in Go, one might also be tempted to use goroutines to process data in parallel. For example, you might write the following code:
|
||||||
```go
|
```go
|
||||||
for val := range values {
|
for _, val := range values {
|
||||||
go val.MyMethod()
|
go val.MyMethod()
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@ -20,7 +20,7 @@ for val := range values {
|
|||||||
or if you wanted to process values coming in from a channel in their own goroutines, you might write something like this, using a closure:
|
or if you wanted to process values coming in from a channel in their own goroutines, you might write something like this, using a closure:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
for val := range values {
|
for _, val := range values {
|
||||||
go func() {
|
go func() {
|
||||||
fmt.Println(val)
|
fmt.Println(val)
|
||||||
}()
|
}()
|
||||||
@ -31,7 +31,7 @@ The above for loops might not do what you expect because their ` val ` variable
|
|||||||
|
|
||||||
The proper way to write that closure loop is:
|
The proper way to write that closure loop is:
|
||||||
```go
|
```go
|
||||||
for val := range values {
|
for _, val := range values {
|
||||||
go func(val interface{}) {
|
go func(val interface{}) {
|
||||||
fmt.Println(val)
|
fmt.Println(val)
|
||||||
}(val)
|
}(val)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user