"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
}
は、任意の単純な方法(例えばリフレクションを使用せずに)チェック値はありますか?
方法について_、[OK]:=インターフェース{}(val)で(Somether)。? – c0ming