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}