course/Concurrency/Concurrency.go

23 lines
444 B
Go
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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也都结束了
}