Dog*_*Dog 5 types overloading interface go
我找到了一个接口,里面有一个叫做的方法_
.我尝试实现它,但它不起作用:
package main
func main() {}
func ft(t T) { fi(t) }
func fi(I) {}
type I interface {
_() int
}
type T struct {}
func (T) _() int { return 0 }
func (T) _(int) int { return 0 }
Run Code Online (Sandbox Code Playgroud)
$ go run a.go
./a.go:4: cannot use t (type T) as type I in function argument:
T does not implement I (missing _ method)
Run Code Online (Sandbox Code Playgroud)
我也尝试添加重载方法,_(int)
但这也不起作用:
package main
func main() {}
type I interface {
_() int
_(int) int
}
type T struct {}
func (T) _() int { return 0 }
func (T) _(int) int { return 0 }
Run Code Online (Sandbox Code Playgroud)
$ go run a.go
# command-line-arguments
./a.go:12: internal compiler error: sigcmp vs sortinter _ _
Run Code Online (Sandbox Code Playgroud)
为什么?这种_
方法的目的是什么?我认为这可能是一种阻止人们实现接口的方法(比如Java中的私有接口)?
_
是"空白标识符"(https://golang.org/ref/spec#Blank_identifier)并具有特殊规则.特别:
空白标识符可以像声明中的任何其他标识符一样使用,但它不引入绑定,因此不会声明.
另请注意,接口部分(https://golang.org/ref/spec#Interface_types)说:
每个方法必须具有唯一的非空名称
所以界面甚至没有效果去; 编译器显然接受它的事实是一个错误.无论哪个包宣布它真的不应该这样做.