带有下划线,内联接口和赋值的变量声明是什么? [英] What is this variable declaration with underscore, inline interface and assignment?
问题描述
Go代码的这段代码是做什么的?
var _ interface {
add(string)error
} =& watchcher {}
我相信& watchcher {}
返回两件事情,第一件被丢弃,第二件被分配...接口?我在Github上找到了 fswatch 中的代码。
此构造将声明一个变量,其中包含空格标识符,其类型由类型文字给出;这种情况下的接口定义。以下是初始化表达式 - 在这种情况下指向复合文本的指针。
片段的总体功能是静态确保 *观察者
满足所述接口,因为 _
变量没有以任何方式实现,只能观察到初始值设定项的任何可能的副作用。无论是静态的(在这种情况下)还是动态的(例如调用一个在运行时分配的函数,比如某些全局变量,注册一个处理程序等)。
What does this snippet of Go code do?
var _ interface {
add(string) error
} = &watcher{}
I believe &watcher{}
returns two things, the first is discarded and the second is assigned to ... an interface? I found the code in fswatch on Github.
This construct will declare a variable with an blank identifier name with type given by a type literal; an interface definition in this case. What follows is in initializer expression - a pointer to a composite literal in this case.
Overall functionality of the snippet is to statically ensure that a *watcher
satisfies the said interface as the _
variable is not materialized in any way and only any possible side effects of the initializer can be observed. Either static (as in this case) or dynamic (like e.g. calling a function which assigns at runtime to, say, some global vars, registers a handler, etc.)
这篇关于带有下划线,内联接口和赋值的变量声明是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!