shapes
インターフェースは何*rect
道具であるが、それは、具体的なタイプ*rect
ではありません。それは、どんなインタフェースと同様に、それを満たす任意の型を渡すことを可能にするメソッドのセットです。一時的な訪問者のステッカーを渡してビルの上に上がるようにします。
たとえば、ゴーの建物で人間ができることすべてを行い行動できる猿(またはそれが価値のあるもの、イルカ)があれば、彼はガードを通過してエレベーターに上がることができます。しかし、それは彼を遺伝的に人間にすることはない。
Goは静的型です。つまり、同じ基本型を持つ2つの型であっても、型宣言や意識的に型を変換せずに動的に変換または強制変換することはできません。
var a int
type myInt int
var b myInt
a = 2
b = 3
b = a // Error! cannot use a (type int) as type myInt in assignment.
b = myInt(a) // This is ok.
が第二のために私と一緒にこのような状況を想像: - :
type circle struct {
Radius float64
}
func (c *circle) setWidth(w float64) {
c.Radius = w
}
*circle
はと全くまずまずで戻ってあなたのscenerioに今
type MyInt int
type YourInt int
type EveryInt interface {
addableByInt(a int) bool
}
func (i MyInt) addableByInt(a int) bool {
// whatever logic doesn't matter
return true
}
func (i YourInt) addableByInt(a int) bool {
// whatever logic doesn't matter
return true
}
func main() {
// Two guys want to pass as an int
b := MyInt(7)
c := YourInt(2)
// Do everything an `EveryInt` requires
// and disguise as one
bi := EveryInt(b)
ci := EveryInt(c)
// Hey, look we're the same! That's the closest
// we can get to being an int!
bi = ci // This is ok, we are EveryInt brotherhood
fmt.Println(bi) // bi is now 2
// Now a real int comes along saying
// "Hey, you two look like one of us!"
var i int
i = bi // Oops! bi has been made
// ci runs away at this point
}
を*circle
がshapes
を実装するに沿って来ると想像shapes
ですが、はありません。ではありません。。インターフェイスは、基になる型のプロパティに直接アクセスすることはできませんが、実装されたメソッドセットを介してのみ行うことができます。
var r *rect
// Verify `s` is in fact a `*rect` under the hood
if r, ok := s.(*rect); ok {
fmt.Println(r.Width)
}
これが行くような静的型付け言語はより常に高速である理由の核心である:そのインスタンスが具体的な形になるようにプロパティにアクセスするためには、型アサーションは、インターフェイス上で必要とされますダイナミックに型付けされたものです。ほとんどの場合、タイプ強制を動的に処理するために何らかのリフレクションが使用されます。
オフトピック:良いスタイルは、集合/コレクションになるまで、クラス/構造体名に単数形を使用することです。したがって、あなたの場合、 'shapes'ではなく' shape'がより適切でしょう。 –