26 lines
461 B
Go
26 lines
461 B
Go
package main
|
||
|
||
import (
|
||
"fmt"
|
||
"time"
|
||
)
|
||
|
||
func hello() {
|
||
fmt.Println("hello")
|
||
}
|
||
|
||
// 主goroutine开启,之后所有的子协程都会自行退出
|
||
func main() {
|
||
|
||
//通过匿名函数开启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)
|
||
|
||
}
|