忽略输出的golang nil指针分配-(*)(nil) [英] golang nil pointer assignment with ignored output - (*)(nil)
问题描述
在经历此教程时,会发现如何编写FUSE文件系统,我遇到了这个神秘的任务:
while going through this tutorial on writing FUSE filesystems in go, I came across this cryptic assignment:
var _ fs.Node = (*Dir)(nil)
有人可以解释这种语法的原理吗?它在声明它的上下文中如何适应?据我了解,赋值的结果实际上被忽略了(右手表达式甚至会导致什么?nir Dir指针?)
Can someone explain the mechanics of this syntax, and how does it fit in the context in which it is declared? As I understand the outcome of the assignment is actually ignored (and what does the right hand expression even result in? a nil Dir pointer?)
推荐答案
这使编译器检查类型*Dir
是否满足fs.Node
接口.
This makes the compiler check if type *Dir
fulfills fs.Node
interface.
获取一个nil
指针,使其成为*Dir
指针,并将其分配给接口类型fs.Node
的未命名变量.由于我们从不使用此变量,因此必须将其命名为空.
Take a nil
pointer, make it a *Dir
pointer and assign it to an unnamed variable of interface type fs.Node
. Since we never use this variable, we have to make it unnamed.
这篇关于忽略输出的golang nil指针分配-(*)(nil)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!