doujing2497 2018-10-12 02:46
浏览 90
已采纳

如何使用golang中的函数将interface {}值转换为静态类型的值?

In standard library, I can do JSON conversion to a typed object using pointer. The question now is, how do I create a similar method like json.Marshal to convert an v interface{} to typed object? Do I need to use reflect in doing this?

Please see below code snippets, and I am looking for somebody who could fill in the TODO in the home package. Thank you.

package main

import (
    "encoding/json"
    "fmt"

    "./home"
)

type Dog struct {
    Name         string
    FavoriteGame string
}

func (dog Dog) Greet() {
    dog.Bark()
}

func (dog Dog) Bark() {
    if len(dog.Name) == 0 {
        panic("This dog has no name!")
    }
    fmt.Printf("%s < Wo! Wo! %s!
", dog.Name, dog.FavoriteGame)
}

type Cat struct {
    Name         string
    FavoriteMeat string
}

func (cat Cat) Greet() {
    cat.Purr()
}

func (cat Cat) Purr() {
    if len(cat.Name) == 0 {
        panic("This cat has no name!")
    }
    fmt.Printf("%s < purrrrrrr... %s...
", cat.Name, cat.FavoriteMeat)
}

func main() {
    // JSON decoder works here
    j := []byte(`{"Name": "Jack", "FavoriteGame": "Swim"}`)
    var jack Dog
    json.Unmarshal(j, &jack)
    jack.Bark()

    // Similarly, how do I implement `home` to make the below work?
    dogHome := home.Home{Dog{Name: "Bullet", FavoriteGame: "Catch The Ball"}}
    var bullet Dog
    dogHome.GetPet(&bullet)
    bullet.Bark()

    catHome := home.Home{Cat{Name: "Ball", FavoriteMeat: "Tuna"}}
    var ball Cat
    catHome.GetPet(&ball)
    ball.Purr()
}

The other package:

package home

type Pet interface {
    Greet()
}

type Home struct {
    Pet Pet
}

func (h Home) GetPet(v interface{}) {
    // TODO: What should I do here?
    v = h.Pet
}
  • 写回答

1条回答 默认 最新

  • 普通网友 2018-10-12 03:48
    关注

    Use the reflect package:

    func (h Home) GetPet(v interface{}) {
        reflect.ValueOf(v).Elem().Set(reflect.ValueOf(h.Pet))
    }
    

    The expression reflect.ValueOf(v).Elem() is the reflect value for what the argument points to. The Set method sets that value.

    Run it on the playground

    A better approach is to use type assertions directly in the application:

    dogHome := Home{Dog{Name: "Bullet", FavoriteGame: "Catch The Ball"}}
    bullet := dogHome.Pet.(Dog)
    bullet.Bark()
    
    catHome := Home{Cat{Name: "Ball", FavoriteMeat: "Tuna"}}
    ball := catHome.Pet.(Cat)
    ball.Purr()
    

    Run it on the playground

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)