* 伊藤和也 <kazya.ito.dr...@gmail.com> [190123 00:27]: > I found the explanation "All types implement the empty interface". > https://golang.org/ref/spec#Interface_types > So an empty interface also implements an empty interface? and again the > empty interface implements an empty interface and the empty inter....... > What I imagine is like this below. > : > : > : > empty interface > | > empty interface > | > empty interface > | | | > int float64 bool > > In java, Object class is the top. > > Object > | > Creture > | | > Animal Human > | | > Dog Cat
Francisco's answer is very good, but I think there is a lot of confusion over variables of an interface type containing values of another interface type. This does not happen. When assigning a value X of an interface type A to a variable Y of another interface type B, only the dynamic type of X is retained in Y. The variable Y contains no information that the value assigned to it was previously held in a variable of type A (i.e. had a different interface type). The dynamic type of an interface value is NEVER an interface type. Repeat the above statement over and over until it sinks in. At https://golang.org/ref/spec#Variables the spec defines the dynamic type of an interface variable as "the concrete type of the value assigned to the variable at run time", but it never defines the term "concrete type". The approximate definition of "concrete type" (and thus "dynamic type") is the underlying non-interface type of the value. This definition is implied by the fact that there is no way to create a value of an interface type (other than nil, which is given as an explicit exception in the definition of "dynamic type") except by assignment from a type that implements the interface. The type of the value being assigned is either an interface type or a non-interface type. If it is an interface type, this is a recursive definition, so the dynamic type of the result of the assignment must be the dynamic type of the value being assigned. Keep recursing on this until the value being assigned is not an interface type, and you end up with the implication that the dynamic type must be a non-interface type. To be pedantic, Jan's answer is wrong. Instances do not implement interfaces, types do (both interface and concrete types). Note under https://golang.org/ref/spec#Interface_types the statement "Such a type is said to implement the interface". Also note that the assignability rules only make sense if you are talking about the type of a value. The following would not be legal if an interface type could not implement another interface type (because the instance of «a» cannot be known at compile time, at least not easily in a more complicated example where the value of a is the result of invoking a function from another package): https://play.golang.org/p/DBj6Jczj3OM type A interface { foo() goo() } type B interface { foo() } type X struct {} func (x X) foo() {} func (x X) goo() {} var a A = X{} var b B = a ...Marvin -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.