dse3168 2013-12-31 05:50
浏览 1238

如何将interface {}转换为golang中的对象?

type Human struct {
     Name string
}

func (t *Human) GetInfo() {
    fmt.Println(t.Name)
}

func main() {
    var p1 interface{}
    p1 = Human{Name:"John"}
    //p1.GetInfo() 
}

now,p1's typs is interface{}, but i want get a Human object.

How to do? i can call p1.GetInfo()

  • 写回答

2条回答 默认 最新

  • dragon8997474 2013-12-31 05:53
    关注

    You can do a type assertion inline:

    p1.(*Human).GetAll()
    

    http://play.golang.org/p/ldtVrPnZ79

    Or you can create a new variable to hold a Human type.

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度