golang 声明变量中的下划线是什么意思
在 Gin 源码中 有一行代码如下
var _ IRouter = &RouterGroup{}
乍一看, 是一个 赋值 操作, 但是前面又使用了 空白描述符(下划线) 。这是什么意思呢?
答案是:接口类型检查
在 《Effective GO》 Interface Check 中的描述有相关描述。全文如下。
One place this situation arises is when it is necessary to guarantee within the package implementing the type that it actually satisfies the interface. If a type-for example,
json.RawMessage
- needs a custom JSON representation, it should implementjson.Marshaler
, but there are no static conversions that would cause the compiler to verify this automatically. If the type inadvertently fails to satisfy the interface, the JSON encoder will still work, but will not use the custom implementation. To guarantee that the implementation is correct, a global declaration using the blank identifier can be used in the package:
var _ json.Marshaler = (*RawMessage)(nil)
In this declaration, the assignment involving a conversion of a
*RawMessage
to aMarshaler
requires that*RawMessage
implementsMarshaler
, and that property will be checked at compile time. Should thejson.Marshaler
interface change, this package will no longer compile and we will be on notice that it needs to be updated.The appearance of the blank identifier in this construct indicates that the declaration exists only for the type checking, not to create a variable. Don’t do this for every type that satisfies an interface, though. By convention, such declarations are only used when there are no static conversions already present in the code, which is a rare event.
简单总结一下
- 假设已有一个 接口
json.Marshaler
, 我们自己编码创建了一个RawMessage
对象。 - 如果
RawMessage
对象能满足json.Marshaler
接口一切皆大欢喜, 但是如果不满足, 那么在运行中就可能出现严重异常。然而, 在 编码阶段 编译器并不能 自动发现 用户对象是否满足接口。 - 因此, 使用了
var TheInterface = *CustomStruct{}
(不满足不能赋值) 这种方式进行编码阶段的验证。但是 golang 特性, 声明了的变量必须要使用。 - 为了解决 声明但不使用 的情况, 引入了 空白描述符
_
下划线 解决这个问题。有了空白描述后, 行为就从赋值 变更为 检查而不创建变量 。var _ TheInterface = *CustomStruct{}
- 最后官方提醒, 这种 奇怪 行为不要乱用, 只用在 那些不能 静态检查 的对象上面。
相关文章
- 分享一个自带翻译的Linux系统连接工具 FinalShell
- 新版本 Kali Linux系统 如何切换到root用户,以及对root 用户重新设置密码
- Kali Linux(2021.2)系统 安装中文输入法
- KaliLinux(2021.2) 系统如何切换中文语言
- KaliLinux-常用命令-进程-命令
- KaliLinux-常用命令-服务-命令
- KaliLinux-MSF-常用命令
- KaliLinux-剪切板攻击-PasteJacker工具的安装
- CentOS7-命令-关闭selinux
- PHP内置协议(支持的协议和封装协议)
- phpStudy 2016搭建-pikachu靶场
- 信息收集-DNS域传送漏洞工具-KaliLinux系统自带(dnsenum、dig)
- Linux 下安装 Nginx
- 教你一招,解决Github图片不显示问题(2021.1.20测试可用)
- Linux 学习笔记
- Github下载release附件变相提速方法
- 流程图软件 drawio 免费 github开源
- Git多人协作
- Linux 下 Too many open files 问题排查与解决
- Linux 下安装 fail2ban