我正在对Go二进制文件进行反向工程,并遇到了函数selectnbrecv
。但是,我不了解documentation。如果有人可以向我解释该功能的上下文和操作,我将不胜感激。谢谢!
最佳答案
它是Go运行时内部实现,用于非阻塞地从 channel 接收:
当 channel 有任何事件时,selectnbrecv
函数返回true
,否则返回false
(对于nil
channel ,返回false
):
它在 channel 上接收并将接收到的数据写入v
(如果&v
不为nil,在这种情况下,接收到的数据将被忽略。)如果没有可用数据,则返回false
。否则,如果关闭 channel ,则将v
归零并返回true
。否则,用数据填充v
并返回true
。
// compiler implements
//
// select {
// case v = <-c:
// ... foo
// default:
// ... bar
// }
//
// as
//
// if selectnbrecv(&v, c) {
// ... foo
// } else {
// ... bar
// }
//
func selectnbrecv(elem unsafe.Pointer, c *hchan) (selected bool) {
selected, _ = chanrecv(c, elem, false) // false for non blocking
return
}
关于go - Go的selectnbrecv函数到底做什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65266072/