Go Tips (i)-Get channel properties

Source: Internet
Author: User

Overview

Here is a method, you can not take the data in the channel, the premise to see if the channel is closed, whether blocking, buffer size, the current amount of buffer data in the channel.
Here you need to know about Golang's memory model , and then use the pointer to check out the corresponding value.

The structure of the channel is in chan.go :

type hchan struct {    qcount   uint           // total data in the queue    dataqsiz uint           // size of the circular queue    buf      unsafe.Pointer // points to an array of dataqsiz elements    elemsize uint16    closed   uint32    //... 以下字段没有用上,先省略}

As can be seen from the above, now to take out from out:

    • qcount: The total number of data in the queue
    • dataqsiz: The maximum length of a queue
    • closed: whether to close

Realize

First we define a struct to correspond to the Hchan. (Because the field length in the Hchan is variable, qcount dataqsiz it is uint OK to say, mainly take closed in the interval unint16 , so here to steal a lazy)

type hchan struct {    qcount   uint           // total data in the queue    dataqsiz uint           // size of the circular queue    buf      unsafe.Pointer // points to an array of dataqsiz elements    elemsize uint16    closed   uint32}

The omitted field is not used here, so the direct removal is done, the above attribute order and the type hchan must be consistent with the original.
Here we want to generalize it so that the packet is received by a method interface{} , such as the channel to which interface{} it will be parsed at the bottom of the Golang eface :

type eface struct {    _type *_type    data  unsafe.Pointer}

So first position the pointer to eface.data the data space that you will point to the custom hchan :

i := (*[2]uintptr)(unsafe.Pointer(&c))h := (*hchan)(unsafe.Pointer(i[1]))

hThe median value is now injected.

Appendix

 type hchan struct {qcount UINT//Total data in the queue dataqsiz uint Size of the circular queue buf unsafe. Pointer//points to an array of dataqsiz elements elemsize uint16 closed Uint32}type chaninfo struct {closed BOOL//whether to close Len UINT//channel data Volume CAP UINT//channel capacity block bool//is blocked} func chanstatus (c int erface{}) (*chaninfo, error) {V: = reflect. ValueOf (c) if V.type (). Kind ()! = reflect. Chan {return nil, errors. New ("Type must be Channel")} I: = (*[2]uintptr) (unsafe. Pointer (&C)) H: = (*hchan) (unsafe.        Pointer (I[1])) return &chaninfo{Cap:h.dataqsiz, len:h.qcount, closed:h.closed = = 1, Block:h.qcount >= H.dataqsiz,}, nil}  

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.