當傳參是接口類型,需要強制類型轉換,
如下:
package main
import "fmt"
type bbc struct {
a int32
}
func main() {
k(bbc{a:100})
}
func k(p interface{}) {
if msg, ok := p.(bbc); ok {
fmt.Println(msg.a)
}
}
或
package main
import "fmt"
type bbc struct {
a int32
}
func main() {
k(&bbc{a:100}) // 注意,這里對應的強制轉化類型
}
func k(p interface{}) {
if msg, ok := p.(*bbc); ok {
fmt.Println(msg.a)
}
}
