• 技术文章 >后端开发 >Golang

    初探Go语言中的Goroutine和channel

    青灯夜游青灯夜游2023-02-02 20:18:01转载494

    本篇文章带大家初步了解Go语言中的Goroutine和channel,希望对大家有所帮助!

    Go 语言的 CSP 并发模型的实现包含两个主要组成部分:一个是 Goroutine,另一个是 channel。本文将会介绍它们的基本用法和注意事项。

    Goroutine

    GoroutineGo 应用的基本执行单元,它是一种轻量的用户级线程,其底层是通过 coroutine(协程)去实现的并发。众所周知,协程是一种运行在用户态的用户线程,因此 Goroutine 也是被调度于 Go 程序运行时。

    基本用法

    语法:go + 函数/方法

    通过 go 关键字 + 函数/方法 可以创建一个 Goroutine

    代码示例:

    import (
       "fmt"
       "time"
    )
    
    func printGo() {
       fmt.Println("具名函数")
    }
    
    type G struct {
    }
    
    func (g G) g() {
       fmt.Println("方法")
    }
    
    func main() {
       // 基于具名函数创建 goroutine
       go printGo()
       // 基于方法创建 goroutine
       g := G{}
       go g.g()
       // 基于匿名函数创建 goroutine
       go func() {
          fmt.Println("匿名函数")
       }()
       // 基于闭包创建 goroutine
       i := 0
       go func() {
          i++
          fmt.Println("闭包")
       }()
       time.Sleep(time.Second) // 避免 main goroutine 结束后,其创建的 goroutine 来不及运行,因此在此休眠 1 秒
    }

    执行结果:

    闭包
    具名函数
    方法
    匿名函数

    当多个 Goroutine 存在时,它们的执行顺序是不固定的。因此每次打印的结果都不相同。

    由代码可知,通过 go 关键字,我们可以基于 具名函数 / 方法 创建 goroutine,也可以基于 匿名函数 / 闭包 创建 goroutine

    那么 Goroutine 是如何退出的呢?正常情况下,只要 Goroutine 函数执行结束,或者执行返回,意味着 Goroutine 的退出。如果 Goroutine 的函数或方法有返回值,在 Goroutine 退出时会将其忽略。

    channel

    channel 在 Go 并发模型中扮演者重要的角色。它可以用于实现 Goroutine 间的通信,也可以用来实现 Goroutine 间的同步。

    channel 的基本操作

    channel 是一种复合数据类型,声明时需要指定 channel 里元素的类型。

    声明语法:var ch chan string

    通过上述代码声明一个元素类型为 stringchannel,其只能存放 string 类型的元素。channel 是引用类型,必须初始化才能写入数据,通过 make 的方式初始化。

    import (
       "fmt"
    )
    
    func main() {
       var ch chan string
       ch = make(chan string, 1)
       // 打印 chan 的地址
       fmt.Println(ch)
       // 向 ch 发送 "Go" 数据
       ch <- "Go"
       // 从 ch 中接收数据
       s := <-ch
       fmt.Println(s) // Go
    }

    通过 ch <- xxx 可以向 channel 变量 ch 发送数据,通过 x := <- ch 可以从 channel 变量 ch 中接收数据。

    带缓冲 channel 与无缓冲 channel

    如果初始化 channel 时,不指定容量时,则创建的是一个无缓冲的 channel

    ch := make(chan string)

    无缓冲的 channel 的发送与接收操作是同步的,在执行发送操作之后,对应 Goroutine 将会阻塞,直到有另一个 Goroutine 去执行接收操作,反之亦然。如果将发送操作和执行操作放在同一个 Goroutine 下进行,会发生什么操作呢?看看下述代码:

    import (
       "fmt"
    )
    
    func main() {
       ch := make(chan int)
       // 发送数据
       ch <- 1 // fatal error: all goroutines are asleep - deadlock!
       // 接收数据
       n := <-ch
       fmt.Println(n)
    }

    程序运行之后,会在 ch <- 处得到 fatal error,提示所有的 Goroutine 处于休眠状态,也就是死锁了。为避免这种情况,我们需要将 channel 的发送操作和接收操作放到不同的 Goroutine 中执行。

    import (
       "fmt"
    )
    
    func main() {
       ch := make(chan int)
       go func() {
          // 发送数据
          ch <- 1
       }()
       // 接收数据
       n := <-ch
       fmt.Println(n) // 1
    }

    由上述例子可以得出结论:无缓冲 channel 的发送与接收操作,一定要放在两个不同的 Goroutine 中进行,否则会发生 deadlock 形象。


    如果指定容量,则创建的是一个带缓冲的 channel

    ch := make(chan string, 5)

    有缓冲的 channel 与无缓冲的 chennel 有所区别,执行发送操作时,只要 channel 的缓冲区未满,Goroutine 不会挂起,直到缓冲区满时,再向 channel 执行发送操作,才会导致 Goroutine 挂起。代码示例:

    func main() {
       ch := make(chan int, 1)
       // 发送数据
       ch <- 1
    
       ch <- 2 // fatal error: all goroutines are asleep - deadlock!
    }

    声明 channel 的只发送类型和只接收类型

    通常只发送 channel 类型和只接收 channel 类型,会被用作函数的参数类型或返回值:

    func send(ch chan<- int) {
       ch <- 1
    }
    
    func recv(ch <-chan int) {
       <-ch
    }

    channel 的关闭

    通过内置函 close(c chan<- Type),可以对 channel 进行关闭。

    小结

    本文首先介绍了 Goroutine的创建方式以及其退出的时机是什么。

    其次介绍了如何创建 channel 类型变量的有缓冲与无缓冲的创建方式。需要注意的是,无缓冲的 channel 发送与接收操作,需要在两个不同的 Goroutine 中执行,否则会发送 error

    接下来介绍如何定义只发送和只接收的 channel 类型。通常只发送 channel 类型和只接收 channel 类型,会被用作函数的参数类型或返回值。

    最后介绍了如何关闭 channel,以及关闭之后的一些注意事项。

    【相关推荐:Go视频教程编程教学

    以上就是初探Go语言中的Goroutine和channel的详细内容,更多请关注php中文网其它相关文章!

    声明:本文转载于:掘金社区,如有侵犯,请联系admin@php.cn删除
    专题推荐:go语言 Golang
    上一篇:go语言中协程与线程的区别是什么 下一篇:自己动手写 PHP MVC 框架(40节精讲/巨细/新人进阶必看)

    相关文章推荐

    • go语言中const怎么用• go语言中split方法有什么用• go语言中关键字有多少个• go语言中标识符的含义是什么• go语言空白标识符是什么• go语言预定义标识符有多少个
    1/1

    PHP中文网