ZBLOG

golang make(chan bool)

In Go programming language, make(chan bool) is used to create a channel of boolean values that can be used for communication and synchronization between goroutines.

Channels are a built-in feature in Go that provide a way for goroutines to communicate with each other and synchronize their execution. A channel is essentially a message queue that allows one goroutine to send data to another goroutine, or multiple goroutines, in a thread-safe manner.

The make function creates a new channel with the specified type and returns a reference to it. In this case, make(chan bool) creates a new unbuffered channel of boolean values.

Here’s an example of how you could use this:

func main() {
    ch := make(chan bool)

    go func() {
        fmt.Println("Goroutine starts")
        // do some work
        fmt.Println("Goroutine finishes")
        ch <- true // signal completion
    }()

    <-ch // wait for completion signal
}

In this example, we create a new channel ch of type bool. We then spawn a new goroutine that performs some work and signals its completion by sending true on the channel. Finally, the main goroutine waits for the completion signal by blocking on <-ch, which will block until there is data available on the channel (in this case, when the spawned goroutine sends its completion signal).

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://golang.0voice.com/?id=4940

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?