在Go语言中,每一个并发的执行单元叫作一个goroutine,也叫协程。
当一个程序启动时,其主函数即在一个单独的goroutine中运行,我们叫它main goroutine。
新的goroutine会用go语句来创建。
f() // call f(); wait for it to return
go f() // create a new goroutine that calls f(); don't wait
主函数返回时,所有的goroutine都会被直接打断,程序退出。 除了从主函数退出或者直接终止程序之外,没有其它的编程方法能够让一个goroutine来打断另一个的执行,但是之后可以看到一种方式来实现这个目的,通过goroutine之间的通信来让一个goroutine请求其它的goroutine,并让被请求的goroutine自行结束执行。