忽略输出的golang nil指针分配-(*)(nil) [英] golang nil pointer assignment with ignored output - (*)(nil)

查看:156
本文介绍了忽略输出的golang nil指针分配-(*)(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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆