概述

goroutine 是 Go 程序并发执行的实体,对于初学者来讲,能够简略地将 goroutine 了解为一个 超轻量的线程

当一个程序启动时,只有一个 goroutine 调用 main 函数,称为 主 goroutine, 当 main 函数返回时,
所有 goroutine 都会被终止 (不管其是否运行实现),而后程序退出。

语法规定

关键字 go 启动一个 goroutine (能够了解为在后盾运行一个函数), 须要留神的是: 应用 go 启动的函数没有返回值

# 间接调用一个匿名函数go func() { // 无参数    // do something ...}go func(x int, y bool ...) { // 有参数    // do something ...}
# 调用一个已定义的函数go foo()   // 无参数go bar(x int, y bool ...)  // 有参数

例子

间接调用一个匿名函数

package mainimport (    "fmt"    "time")func main() {    // 3 个 goroutine 是并发运行的,所以程序不肯定是 1, 2, 3    // 读者能够多运行几次,看看输入后果    go func() {        fmt.Println("goroutine 1")    }()    go func() {        fmt.Println("goroutine 2")    }()    go func() {        fmt.Println("goroutine 3")    }()    // 这一行代码不可省略    // 如果省略掉,意味着主过程不期待 3 个 goroutine 执行实现就退出了,也就不会有 goroutine 的输入信息了    // 读者能够正文掉这行代码,而后运行看看输入后果    time.Sleep(1 * time.Second)}// $ go run main.go// 输入如下, 3 个 goroutine 是并发运行的,程序不肯定,所以你的输入可能和这里的不一样/**  goroutine 3  goroutine 1  goroutine 2*/

调用 time.Sleep() 睡眠期待 3 个 goroutine 执行实现,尽管达到了演示成果,然而有很多潜在问题。
更好的解决方案请看 waitgroup。

调用一个已定义的函数

package mainimport (    "fmt"    "time")func foo() {    fmt.Println("goroutine foo")}func bar() {    fmt.Println("goroutine bar")}func fooBar(s string) {    fmt.Printf("goroutine %s\n", s)}func main() {    // 3 个 goroutine 是并发运行的,所以程序不肯定是 1, 2, 3    // 读者能够多运行几次,看看输入后果    go foo()    go bar()    go fooBar("fooBar")    // 这一行代码不可省略    // 如果省略掉,意味着主过程不期待 3 个 goroutine 执行实现就退出了,也就不会有 goroutine 的输入信息了    // 读者能够正文掉这行代码,而后运行看看输入后果    time.Sleep(1 * time.Second) }// $ go run main.go// 输入如下, 3 个 goroutine 是并发运行的,程序不肯定,所以你的输入可能和这里的不一样/**  goroutine fooBar  goroutine foo  goroutine bar*/

获取并发线程数量

GOMAXPROCS() 取得并发的线程数量,在 CPU 核大于 1 个的状况下,零碎会尽可能调度等于外围数的线程并行运行。

package mainimport (    "fmt"    "runtime")func main() {    fmt.Printf("GOMAXPROCS = %d\n", runtime.GOMAXPROCS(0))}// $ go run main.go// 输入如下,笔者的机器 CPU 是 8 核,你的输入可能和这里的不一样/**  GOMAXPROCS = 8*/

扩大浏览

  1. 协程 - 维基百科
  2. 线程 - 维基百科
  3. Go 圣经 - 第 8 章

分割我