我讀過「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