golang接口的这个用法,我到还真是百思不得其解,望解惑

ddxx11223 · · 975 次点击
叫做:类型断言,可以理解为类型转换。
#1
更多评论
规范里面是这里定义的: “An interface type specifies a method set called its interface. A variable of interface type can store a value of ***any*** type with a method set that is any superset of the interface” 这里的any也包括其它interface类型, 任何类型只要它实现了某个interface A的所有接口,这个类型的变量就可以转换为A类型
#2
x.(T) If T is an interface type, x.(T) asserts that the dynamic type of x implements the interface T. https://golang.org/ref/spec#Type_assertions
#3