前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >golang 声明变量中的下划线是什么意思

golang 声明变量中的下划线是什么意思

作者头像
老麦
发布2022-12-24 09:38:42
1.1K0
发布2022-12-24 09:38:42
举报
文章被收录于专栏:Go与云原生Go与云原生

在 Gin 源码中 有一行代码如下

代码语言:javascript
复制
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 implement json.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:

代码语言:javascript
复制
var _ json.Marshaler = (*RawMessage)(nil)

In this declaration, the assignment involving a conversion of a *RawMessage to a Marshaler requires that *RawMessage implements Marshaler, and that property will be checked at compile time. Should the json.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.

简单总结一下

  1. 假设已有一个 接口 json.Marshaler, 我们自己编码创建了一个 RawMessage 对象。
  2. 如果 RawMessage 对象能满足 json.Marshaler 接口一切皆大欢喜, 但是如果不满足, 那么在运行中就可能出现严重异常。然而, 在 编码阶段 编译器并不能 自动发现 用户对象是否满足接口。
  3. 因此, 使用了 var TheInterface = *CustomStruct{} (不满足不能赋值) 这种方式进行编码阶段的验证。但是 golang 特性, 声明了的变量必须要使用。
  4. 为了解决 声明但不使用 的情况, 引入了 空白描述符 _ 下划线 解决这个问题。有了空白描述后, 行为就从赋值 变更为 检查而不创建变量 。 var _ TheInterface = *CustomStruct{}
  5. 最后官方提醒, 这种 奇怪 行为不要乱用, 只用在 那些不能 静态检查 的对象上面。
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2021-08-28,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 熊猫云原生Go 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档