从翻译上调整了一下英文原著解释中的因果顺序 (#606)

* 建议从翻译上调转了一下英文原著解释中的因果顺序

英文原著:
> Notice the use of the closure:  the current i, xi are passed to the closure as parameters, masking the i, xi variables from the outer for-loop.  This allows each goroutine to have its own copy of i, xi; otherwise, the next iteration of the for-loop would update i, xi in all goroutines. On the other hand, the res slice is not passed to the closure, since each goroutine does not need a separate copy of it.  The res slice is part of the closure’s environment but is not a parameter

原译文:
>注意闭合:i、xi 都是作为参数传入闭合函数的,从外层循环中隐藏了变量 i 和 xi。让每个协程有一份 i 和 xi 的拷贝;另外,for 循环的下一次迭代会更新所有协程中 i 和 xi 的值。切片 res 没有传入闭合函数,因为协程不需要单独拷贝一份。切片 res 也在闭合函数中但并不是参数。

 我的建议:
>注意上述代码中闭合函数的用法:`i`、`xi` 都是作为参数传入闭合函数的,这一做法使得每个协程有一份 `i` 和 `xi` 的拷贝,从而向闭合函数内部隐藏了外层循环中的变量 `i` 和 `xi`;否则,for 循环的下一次迭代会更新所有协程中 `i` 和 `xi` 的值。另一方面,切片 `res` 没有传入闭合函数,因为协程不需要`res`的单独拷贝。切片 `res` 也在闭合函数中但并不是参数。

* 增加了译者注和一些用词调整
This commit is contained in:
Respawnz
2019-04-26 03:46:37 +08:00
committed by 无闻
parent 4a08451530
commit 43672654ca

View File

@@ -302,7 +302,7 @@ go doSort(s[i:])
<-done <-done
``` ```
下边的代码用完整的信号量模式对长度为N的 float64 切片进行了 N 个` doSomething()` 计算并同时完成,通道 sem 分配了相同的长度(包含空接口类型的元素),待所有的计算都完成后,发送信号(通过放入值)。在循环中从通道 sem 不停的接收数据来等待所有的协程完成。 下边的代码用完整的信号量模式对长度为N的 float64 切片进行了 N 个` doSomething()` 计算并同时完成,通道 sem 分配了相同的长度(包含空接口类型的元素),待所有的计算都完成后,发送信号(通过放入值)。在循环中从通道 sem 不停的接收数据来等待所有的协程完成。
```go ```go
type Empty interface {} type Empty interface {}
@@ -322,7 +322,8 @@ for i, xi := range data {
for i := 0; i < N; i++ { <-sem } for i := 0; i < N; i++ { <-sem }
``` ```
注意闭合`i``xi` 都是作为参数传入闭合函数的,从外层循环中隐藏了变量 `i``xi`。让每个协程有一份 `i``xi` 的拷贝;另外for 循环的下一次迭代会更新所有协程中 `i``xi` 的值。切片 `res` 没有传入闭合函数,因为协程不需要单独拷贝一份。切片 `res` 也在闭合函数中但并不是参数。 注意上述代码中闭合函数的用法`i``xi` 都是作为参数传入闭合函数的,这一做法使得每个协程(译者注:在其启动时)获得一份 `i``xi` 的单独拷贝,从而向闭合函数内部屏蔽了外层循环中的 `i``xi`变量;否则for 循环的下一次迭代会更新所有协程中 `i``xi` 的值。另一方面,切片 `res` 没有传入闭合函数,因为协程不需要`res`单独拷贝。切片 `res` 也在闭合函数中但并不是参数。
## 14.2.8 实现并行的 for 循环 ## 14.2.8 实现并行的 for 循环