Interface
shapes implements *rect , but it is not a specific *rect type. It, like any interface, is a set of methods that allow any type that satisfies its passage, for example, to provide a temporary business card for him to climb the building.
For example, if there is a monkey (or for the fact that it stands, a dolphin) that can act and do everything that a person can in the Guo building, he can go through security and take the elevator. However, this does not make him genetically human.
Go is statically typed, which means that even two types with the same basic type cannot dynamically convert or force each other without type approval or consciously convert the type.
var a int type myInt int var b myInt a = 2 b = 3 b = a
Introduce yourself to me for a second in this situation:
type MyInt int type YourInt int type EveryInt interface { addableByInt(a int) bool } func (i MyInt) addableByInt(a int) bool {
Now back to your scenerio - imagine *circle implements shapes :
type circle struct { Radius float64 } func (c *circle) setWidth(w float64) { c.Radius = w }
*circle is completely skipped as shapes , but it does not have the Width property, because it is not *rect . An interface cannot directly access a property of a base type, but can only do this through an implemented set of methods. Accessing a property in an interface requires a type statement, so the instance becomes a concrete type:
var r *rect
This is at the heart of why a statically typed language like Go is always faster than dynamically typed counterparts, which will almost always use some kind of reflection to dynamically control the type for you.