23 lines
444 B
Go
23 lines
444 B
Go
package main
|
||
|
||
import (
|
||
"fmt"
|
||
"time"
|
||
)
|
||
|
||
func hello(i int) {
|
||
fmt.Println("hello world", i)
|
||
}
|
||
|
||
// 程序启动之后会创建一个主goroutine去执行
|
||
func main() {
|
||
for i := 0; i < 10; i++ {
|
||
go func(i int) {
|
||
fmt.Println("hello world", i)
|
||
}(i)
|
||
} //开启一个单独的goroutine 去执行hello函数(任务)
|
||
fmt.Println("main")
|
||
time.Sleep(1 * time.Second)
|
||
//main函数结束了 由main函数启动的goroutine也都结束了
|
||
}
|