golang中关键字var之后的下划线和接口名称是什么意思? [英] what does a underscore and interface name after keyword var in golang mean?
问题描述
http://golang.org/src/pkg/database/sql/driver/types.go
30 type ValueConverter interface {
31 // ConvertValue converts a value to a driver Value.
32 ConvertValue(v interface{}) (Value, error)
33 }
54 var Bool boolType
55
56 type boolType struct{}
57
58 var _ ValueConverter = boolType{}
59
60 func (boolType) String() string { return "Bool" }
61
62 func (boolType) ConvertValue(src interface{}) (Value, error) {....}
如上所述,我知道ValueConverter是一个接口名称,第58行似乎是声明boolType实现接口ValueConverter,这是否有必要?我删除了第58行,代码运行良好
As above, I known that ValueConverter is an interface name, line 58 seems like to declare that boolType implement interface ValueConverter, does that necessary? I deleted line 58, the code works well
推荐答案
它提供了一个静态(编译时)检查 boolType
满足 ValueConverter
界面。用作变量名称的 _
告诉编译器有效地丢弃RHS值,但是如果它有任何副作用,则进行类型检查并评估它,但是匿名变量本身不占用任何进程空间。
It provides a static (compile time) check that boolType
satisfies the ValueConverter
interface. The _
used as a name of the variable tells the compiler to effectively discard the RHS value, but to type-check it and evaluate it if it has any side effects, but the anonymous variable per se doesn't take any process space.
这是一个方便的构造,当开发时,接口的方法集和/或类型实现的方法经常改变。该构造可以作为防止忘记匹配类型和接口的方法集合的意图,其目的是让它们兼容。它有效地防止去安装
一个有这种省略的破碎(中间)版本。
It is a handy construct when developing and the method set of an interface and/or the methods implemented by a type are frequently changed. The construct serves as a guard against forgetting to match the method sets of a type and of an interface where the intent is to have them compatible. It effectively prevents to go install
a broken (intermediate) version with such omission.
这篇关于golang中关键字var之后的下划线和接口名称是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!