我读过“Effective Go”和其他Q &就像这样:golang interface compliance compile type check,但是我无法正确理解如何使用这种技术。检查值是否实现接口的说明。 Golang
请参见例如:
type Somether interface {
Method() bool
}
type MyType string
func (mt MyType) Method2() bool {
return true
}
func main() {
val := MyType("hello")
//here I want to get bool if my value implements Somether
_, ok := val.(Somether)
//but val must be interface, hm..what if I want explicit type?
//yes, here is another method:
var _ Iface = (*MyType)(nil)
//but it throws compile error
//it would be great if someone explain the notation above, looks weird
}
有没有简单的方法(例如,不使用反射)校验值,如果它实现了一个接口?
怎么样_,确定:=接口{}(VAL)(Somether)。? – c0ming