Go中的赋值声明的LHS下划线 [英] Underscore in LHS of assignment statement in go

查看:49
本文介绍了Go中的赋值声明的LHS下划线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码段的作用是什么?

What does this code snippet do?

var i int
_ = i

我了解使用"_"作为空白标识符,但是上述第二行实现了什么?

I understand the use of "_" as a blank identifier, but what does the second line in the above achieve?

以下是etcd GitHub存储库中的示例:

Here is an example from the etcd GitHub repository: etcd

推荐答案

代码为

The code is machine generated. The generator added the statements _ = i to avoid unused variable declarations in the case where there's nothing to marshal.

代码生成器的作者可能发现添加空白赋值语句比不需要时省略变量更容易.

The author of the code generator probably found it easier to add the blank assignment statements than to omit the variables when not needed.

这篇关于Go中的赋值声明的LHS下划线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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