How to block functions in the go language

Source: Internet
Author: User
Brief introduction

When you use go, you usually use the goroutine, but there are often problems such as the following.

Func Main () {
    go dosomething ()
    go dosomething ()
}

This type of writing usually causes the program to flash through. At this point it may be necessary to block the main function so that he does not exit. Some would say that you can use an empty for loop like in C + +. This is not possible, go does not allow the use of empty loop statements. using Select

The go language gives a syntactic sugar, which can be implemented by using a SELECT empty statement.

Func Main () {
    go dosomething ()
    go dosomething ()
    select{}
}

This place finds the block () in the runtime by using the assembly code to find the select null in the bottom go compiler. As shown in figure

The Gopark in the source code means that the current goroutine is placed in a wait state.

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.