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 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。
  • ¥15 各位 帮我看看如何写代码,打出来的图形要和如下图呈现的一样,急
  • ¥30 c#打开word开启修订并实时显示批注
  • ¥15 如何解决ldsc的这条报错/index error