course/Concurrency/Concurrency.go

26 lines
461 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() {
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)
}