course/Concurrency/Concurrency.go

23 lines
444 B
Go
Raw Normal View History

2024-07-03 02:05:02 +08:00
package main
2024-07-03 18:35:02 +08:00
import (
"fmt"
"time"
)
2024-07-03 19:57:07 +08:00
func hello(i int) {
fmt.Println("hello world", i)
2024-07-03 18:35:02 +08:00
}
2024-07-03 19:57:07 +08:00
// 程序启动之后会创建一个主goroutine去执行
2024-07-03 02:05:02 +08:00
func main() {
2024-07-03 18:35:02 +08:00
for i := 0; i < 10; i++ {
2024-07-03 19:57:07 +08:00
go func(i int) {
fmt.Println("hello world", i)
}(i)
} //开启一个单独的goroutine 去执行hello函数任务
fmt.Println("main")
time.Sleep(1 * time.Second)
//main函数结束了 由main函数启动的goroutine也都结束了
2024-07-03 02:05:02 +08:00
}