Nil Channels always block (go language hollow pipe is blocked)

Source: Internet
Author: User

Translated from: https://www.godesignpatterns.com/2014/05/nil-channels-always-block.html

Original Alex Lockwood

In this article, we will discuss the use of the nil channel in Go. Nil Channel is permanently blocked whether it is received or sent:

// Create an uninitialized (nil) channel. var struct {}//  receiving on a nil channel blocks forever. <-ch//  sending on a nil channel would also block forever. struct {}{}

The nil channel does not seem to work and even causes bugs in your project (such as forgetting to initialize with make before using channels). However, you can take advantage of this property in several clever ways, especially if you need to dynamically disable a case in a SELECT statement:

If disableselectcase {ch1 = nil} else {ch1 = nonnilchan}select {Case <-ch1://the case would only be considered if CH1 is not nil.
The case should not be considered until CH1 is nil, <-ch2://this, will has always been considered (assuming CH2 is Non-nil).

The code above illustrates a feature that, when a case in a SELECT statement is a nil channel, it is selectively ignored. If the Boolean type of Disableselectcase is true, nil is assigned to CH1, which prevents CH1 channel from being considered by the SELECT statement.

This feature is also used to prevent empty loops, such as when you need to wait for multiple channels to close:

warning! Spin Loop might Occur!var isClosed1, isClosed2 boolfor!isclosed1 | | !isclosed2 {Select {case _, OK: = <-ch1:if!ok {isClosed1 = True}case _, OK: = <-ch2:if!ok {isClosed2 = true}}}

If one of the two channel in the above code is closed, it will cause a minor bug in your project. Because the closed channel is never blocked, if the ch1 is closed, CH1 will always be ready to be received in the subsequent iteration of the loop. As a result, the program will fall into a dead loop, and there is case <-CH2 may never be able to be executed.

To solve this problem, we can use the Nil channel always blocking feature:

Safely disable channels after they is closed.for ch1! = Nil | | CH2! = Nil {Select {case _, OK: = <-ch1:if!ok {ch1 = Nil}case _, OK: = <-ch2:if!ok {CH2 = nil}}}

  

Nil Channels always block (go language hollow pipe is blocked)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.