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

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

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码