下划线和关键字var后面的接口名称是什么意思?

huangapple go评论80阅读模式
英文:

What does an underscore and interface name after keyword var mean?

问题

type ValueConverter interface {
// ConvertValue converts a value to a driver Value.
ConvertValue(v interface{}) (Value, error)
}

var Bool boolType

type boolType struct{}

var _ ValueConverter = boolType{} // line 58

func (boolType) String() string { return "Bool" }

func (boolType) ConvertValue(src interface{}) (Value, error) {....}

我知道ValueConverter是一个接口名字。第58行似乎声明boolType实现了接口ValueConverter,但这是必要的吗?我删除了第58行,代码仍然正常工作。

英文:

From http://golang.org/src/pkg/database/sql/driver/types.go:

type ValueConverter interface {
	// ConvertValue converts a value to a driver Value.
	ConvertValue(v interface{}) (Value, error)
}

var Bool boolType

type boolType struct{}

var _ ValueConverter = boolType{} // line 58

func (boolType) String() string { return "Bool" }

func (boolType) ConvertValue(src interface{}) (Value, error) {....}

I known that ValueConverter is an interface name. Line 58 seems to declare that boolType implement interface ValueConverter, but is that necessary? I deleted line 58 and the code works well.

答案1

得分: 158

它提供了一个静态(编译时)检查,确保boolType满足ValueConverter接口。变量名_告诉编译器有效地丢弃RHS值,但要对其进行类型检查并评估它是否有任何副作用,但是匿名变量本身不占用任何进程空间。

在开发过程中,当接口的方法集和/或类型实现的方法经常更改时,这是一个方便的构造。该构造用作防止忘记匹配类型和接口的方法集的保护,其中意图是使它们兼容。它有效地防止了使用这种遗漏的(中间)版本进行go install

英文:

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.

答案2

得分: 33

似乎你正在创建一个类型为ValueConverter的虚拟值,将一个新的boolType对象赋给它,然后将其丢弃(这在Go语言中的下划线的意义是,如果你对枚举的索引不感兴趣,可以使用for _, elt := range myRange { ...})。

我猜想这只是一个静态检查,以确保结构体boolType确实实现了ValueConverter接口。这样,当你改变boolType的实现时,如果你破坏了ValueConverter接口的实现,编译器会及早报错,因为它将无法将你的新boolType转换为该接口。

英文:

It seems like you are creating a dummy value of type ValueConverter, assigning a new boolType object to it and then discarding it (which is the meaning of the underscore in go, as in for _, elt := range myRange { ...} if you are not interested in the index of the enumeration).

My guess is that it simply correspond to a static check to ensure that the struct boolType does implement the ValueConverter interface. This way, when you change the implementation of boolType, the compiler will complain early if you broke the implementation of ValueConverter interface as it will be unable to cast your new boolType to this interface.

huangapple
  • 本文由 发表于 2012年11月2日 19:25:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/13194272.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定