如何从接口获取底层指针类型?
package main
import (
"fmt"
)
type Car interface {
Drive() string
}
type MyCar struct {
name string
}
func (MyCar) Drive ( ) string {
return "rum rum"
}
func main() {
var car Car
mycar := &MyCar{name:"mycar"}
car = mycar
mycarptr, err := car.(*MyCar)
mycarvalue, err2 := car.(MyCar)
fmt.Printf( "as ptr failed: %t, as value failed: %t\n", err, err2 )
fmt.Printf( "as ptr: %+v, as value: %+v", mycarptr, mycarvalue)
}