2024-07-03 02:05:02 +08:00
|
|
|
|
package main
|
|
|
|
|
|
2024-07-03 18:35:02 +08:00
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func hello() {
|
|
|
|
|
fmt.Println("hello")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 主goroutine开启,之后所有的子协程都会自行退出
|
2024-07-03 02:05:02 +08:00
|
|
|
|
func main() {
|
|
|
|
|
|
2024-07-03 18:35:02 +08:00
|
|
|
|
//通过匿名函数开启goroutine
|
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
|
go func() {
|
|
|
|
|
fmt.Println(i)
|
|
|
|
|
}() //事实上这个函数中的i去找到的是上面这个循环的i,所以当goroutine启动过快有可能在一个循环中找到i
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
fmt.Println("this is main,2")
|
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
|
|
2024-07-03 02:05:02 +08:00
|
|
|
|
}
|