Go language Select and Chan

Source: Internet
Author: User
This is a created article in which the information may have evolved or changed.

Chan is a very important concept for Golang to communicate or share data between different goroutine, which is the way go recommends sharing data.

Chan has a default deadlock detection mechanism, such as this code

Http://play.golang.org/p/_0UWFWiGOr

Run the time will prompt: Fatal Error:all goroutines is asleep–deadlock!

Chan can be combined with SELECT, and when Chan is combined with SELECT, there is an interesting phenomenon that Chan becomes non-blocking

What's more interesting is time. Timer,chan with SELECT, each time a Select Chan case occurs, the timer is reset. An extreme example, in Select, only the timer and default, the program will be a few seconds to drain the system resources, do not know the calculation is not a bug, the code here:

Http://play.golang.org/p/WYA-0HmUEH

Also, if there is a timer and default in a select, then the timer will never return.

The reason seems to be that every time the select is over, it resets the timer and does not know if my understanding is biased.

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.