vendredi 4 décembre 2015

How to I convert reflect.New's return value back to the original type

I'm using reflection in go and I noticed the oddity expressed below:

package main

import (
        "log"
        "reflect"
)

type Foo struct {
        a int
        b int
}

func main() {
        t := reflect.TypeOf(Foo{})
        log.Println(t) // main.Foo
        log.Println(reflect.TypeOf(reflect.New(t))) // reflect.Value not main.Foo
}

How can I convert the reflect.Value back to main.Foo?

I've provided a go playground for convenience.





Aucun commentaire:

Enregistrer un commentaire