package main
import (
"fmt"
)
func main() {
ch := make(chan string)
for i := 0; i < 5000; i++ {
// go starts a goroutine
go printHelloWorld(i, ch) // go调用函数为并发执行
}
// 主程序拿到返回来的ch输出
for {
msg := <- ch
fmt.Println(msg)
}
}
func printHelloWorld(i int, ch chan string) {
for {
// 把print中的东西传递给ch
ch <- fmt.Sprintf("HelloWorld from goroutine %d!\n",
i);
}
}
Go 实现并发执行
最新推荐文章于 2021-12-29 17:38:26 发布