Golang:永远的通道
Golang: forever channel
有个问题,这里发生了什么?
forever := make(chan bool)
log.Printf(" [*] Waiting for messages. To exit press CTRL+C")
<-forever
该代码创建一个无缓冲通道,并尝试从中接收。
而且因为没有人在上面发送任何东西,所以它本质上是一个永远阻塞的操作。
目的是为了防止 goroutine 结束/返回,很可能是因为有其他 goroutines 同时做一些工作或者他们等待某些事件或传入消息(就像您的日志消息所说)。
而需要的是,如果没有这个,应用程序可能会在不等待其他 goroutine 的情况下退出。也就是说,如果 main
goroutine 结束,程序也会结束。引用自 Spec: Program execution:
Program execution begins by initializing the main package and then invoking the function main. When that function invocation returns, the program exits. It does not wait for other (non-main
) goroutines to complete.
查看此答案以了解更多类似技巧:
有关频道的介绍,请参阅
问题中的代码可能来自RabbitMQ golang教程here。
这是其中的更扩展部分,其中包含我自己的一些建议:
...
// create an unbuffered channel for bool types.
// Type is not important but we have to give one anyway.
forever := make(chan bool)
// fire up a goroutine that hooks onto msgs channel and reads
// anything that pops into it. This essentially is a thread of
// execution within the main thread. msgs is a channel constructed by
// previous code.
go func() {
for d := range msgs {
log.Printf("Received a message: %s", d.Body)
}
}()
log.Printf(" [*] Waiting for messages. To exit press CTRL+C")
// We need to block the main thread so that the above thread stays
// on reading from msgs channel. To do that just try to read in from
// the forever channel. As long as no one writes to it we will wait here.
// Since we are the only ones that know of it it is guaranteed that
// nothing gets written in it. We could also do a busy wait here but
// that would waste CPU cycles for no good reason.
<-forever
有个问题,这里发生了什么?
forever := make(chan bool)
log.Printf(" [*] Waiting for messages. To exit press CTRL+C")
<-forever
该代码创建一个无缓冲通道,并尝试从中接收。
而且因为没有人在上面发送任何东西,所以它本质上是一个永远阻塞的操作。
目的是为了防止 goroutine 结束/返回,很可能是因为有其他 goroutines 同时做一些工作或者他们等待某些事件或传入消息(就像您的日志消息所说)。
而需要的是,如果没有这个,应用程序可能会在不等待其他 goroutine 的情况下退出。也就是说,如果 main
goroutine 结束,程序也会结束。引用自 Spec: Program execution:
Program execution begins by initializing the main package and then invoking the function main. When that function invocation returns, the program exits. It does not wait for other (non-
main
) goroutines to complete.
查看此答案以了解更多类似技巧:
有关频道的介绍,请参阅
问题中的代码可能来自RabbitMQ golang教程here。
这是其中的更扩展部分,其中包含我自己的一些建议:
...
// create an unbuffered channel for bool types.
// Type is not important but we have to give one anyway.
forever := make(chan bool)
// fire up a goroutine that hooks onto msgs channel and reads
// anything that pops into it. This essentially is a thread of
// execution within the main thread. msgs is a channel constructed by
// previous code.
go func() {
for d := range msgs {
log.Printf("Received a message: %s", d.Body)
}
}()
log.Printf(" [*] Waiting for messages. To exit press CTRL+C")
// We need to block the main thread so that the above thread stays
// on reading from msgs channel. To do that just try to read in from
// the forever channel. As long as no one writes to it we will wait here.
// Since we are the only ones that know of it it is guaranteed that
// nothing gets written in it. We could also do a busy wait here but
// that would waste CPU cycles for no good reason.
<-forever