type Empty interface {}
var empty Empty
...
data := make([]float64, N)
res := make([]float64, N)
sem := make(chan Empty, N)
...
for i, xi := range data {
go func (i int, xi float64) {
res[i] = doSomething(i, xi)
sem <- empty
} (i, xi)
}
// wait for goroutines to finish
for i := 0; i < N; i++ { <-sem }
golang goroutine和频道的简单演示
package main
import (
"runtime"
)
func main(){
runtime.GOMAXPROCS(8)
ch := make(chan string)
doneCh := make(chan bool)
go abcGen(ch)
go printer(ch,doneCh)
println("This comes first")
//