大家对go语言陌生吗?大家有没有了解过关于go的协成呢,这里没有废话,直接上
使用go get 可以设置这个变量go env -w GOPROXY=https://goproxy.io,direct
会使go get 更加快
package test_2
import (
"fmt"
"testing"
)
func workers(id int, taskCh chan int, resultCh chan int) {
for task := range taskCh {
fmt.Printf("worker %d start\n", id)
resultCh <- task * 2
}
}
func getTaskData(a []int, taskCh chan int) {
for i := 0; i < len(a); i++ {
taskCh <- a[i]
}
close(taskCh)
}
func TestExecuteAllTask(t *testing.T) {
albums := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}
tasksCh := make(chan int, len(albums))
resultCh := make(chan int, len(albums))
for i := 0; i < 3; i++ {
go workers(i, tasksCh, resultCh)
}
getTaskData(albums, tasksCh)
for a := 0; a < len(albums); a++ {
result := <-resultCh
fmt.Printf("结果为:%d \n", result)
}
}