企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
## 仅需任意任务完成 场景:获取比赛第一名成绩 ~~~ func DoTask(num int) string { fmt.Println("run task", num) return fmt.Sprintf("run task finished %v", num) } func TestOneTaskRunReturn(t *testing.T) { taskNum := 10 fmt.Println("before goroutine num:", runtime.NumGoroutine()) result := make(chan string, taskNum) for i := 1; i < taskNum; i++ { go func(transId int) { taskResult := DoTask(transId) result <- taskResult }(i) } str := <-result time.Sleep(2 * time.Second) fmt.Println("main goroutine result:", str) fmt.Println("after goroutine num:", runtime.NumGoroutine()) } ~~~