代码如下:
```go
package main
import (
"fmt"
"runtime"
)
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
c := make(chan bool, 10)
for i := 0; i < 10; i++ {
go Go(c, i)
}
for i := 0; i < 10; i++ { //如何使用`for-range`循环实现?
<-c
}
}
func Go(c chan bool, index int) {
a := 0
for i := 0; i < 10000000; i++ {
a += i
}
fmt.Println(index, a)
c <- true
}
```
求解。
![D59AFC09-6D42-430E-A74F-7332FA089F92.png](http://studygolang.qiniudn.com/161223/d8e5dcb822871fe99a02f7d81a42f2c9.png)
#3
更多评论
package main
import (
"fmt"
"runtime"
)
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
cs := make([]chan bool, 10)
for i := 0; i < 10; i++ {
cs[i] = make(chan bool, 1)
go Go(cs[i], i)
}
for _, c := range cs {
<-c
}
}
func Go(c chan bool, index int) {
a := 0
for i := 0; i < 10000000; i++ {
a += i
}
fmt.Println(index, a)
c <- true
}
改成slice试试?
#2