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

时间:2018-10-12 02:46:34

标签: go

在标准库中,我可以使用指针将JSON转换为类型化的对象。现在的问题是,如何创建类似json.Marshal的类似方法将v interface{}转换为类型对象?我需要使用reflect吗?

请参见下面的代码段,我正在寻找可以填写TODO包中的home的人。谢谢。

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!\n", 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...\n", 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()
}

另一个包:

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 个答案:

答案 0 :(得分:0)

使用reflect软件包:

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

表达式reflect.ValueOf(v).Elem()是自变量指向的反射值。 Set方法设置该值。

Run it on the playground

更好的方法是直接在应用程序中使用类型断言:

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