[Golang] Simulation implementation of select Multi-Path selection

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed. Sometimes there is a scenario where you wait for multiple events to arrive, and then return as many events as possible, and block the wait if no events arrive. For example, the server waits for a client to establish a connection, or waits for client data to have such an application requirement. In the go language, you can use the Select primitive and its non-blocking (default) branch combination to achieve this functionality:
// 从ch获取尽可能多的数据放到events里,并返回实际数量;如果没有数据就阻塞等待func wait(ch chan int, events []int) int {    count := 0    for count < len(events) {        select {        case x := <-ch:            events[count] = x            count++        default:            if count > 0 {                return count            }            events[count] = <-ch            count++        }    }    return count}
If Plus exit check:
import "errors"func wait(ch chan int, exit chan bool, events []int) (int, error) {    count := 0    for count < len(events) {        select {        case <-exit:            return 0, errors.New("exit")        case x := <-ch:            events[count] = x            count++        default:            if count > 0 {                return count, nil            }            select {            case <-exit:                return 0, errors.New("exit")            case x := <-ch:                events[count] = x                count++            }        }    }    return count, nil}
As you can see, the implementation here has a lot of repetitive code, which is very lengthy and difficult to read. We can use the channel following features to rewrite: 1. Permanent blocking when reading or writing to an empty channel 2. Reads an already closed channel immediately returns a null value
import "errors"var (CLOSED = make(chan int))func init() {close(CLOSED)}func pass(flag bool) chan int {if flag {return CLOSED}return nil}func wait(ch chan int, exit chan bool, events []int) (int, error) {count := 0LOOP:for count < len(events) {select {case <-exit:return 0, errors.New("exit")case x := <-ch:events[count] = xcount++case <-pass(count > 0):break LOOP}}return count, nil}
Now the implementation is relatively clear and concise and easy to read.
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.