``` go
type I interface{}
type II interface{
f1()
}
func g2(a II) {
fmt.Println("xxx")
}
func h3(b I) {
g2(b.(II))
}
type Message struct {
Name string `json:"msg_name"`
}
func (*Message) f1() {
}
func main(){
var m = Message{
Name: "Alice",
}
h3(&m)
}
```
`h3`函数中的`g2(b.(II))`这到底是个什么用法?接口强制转换?不像啊,我知道`b`其实是从`I接口`动态转过来的`*Message`类型,那这个`b.(II)`算啥意思?
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
更多评论
规范里面是这里定义的:
“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