mercredi 4 février 2015

Why does putting a pointer in an interface{} in Go cause reflect to lose the name of the type?

The example below shows what happens when you reflect on an interface {} that is set to an object (g) and a pointer to said object (h). Is this by design, should I expect that my datatype is lost or rather or that I cannot get name of the datatype back when I put my pointer in an interface {}?



package main

import "fmt"
import "reflect"

type Foo struct {
Bar string
}

func main() {
f := Foo{Bar: "FooBar"}
typeName := reflect.TypeOf(f).Name()
fmt.Printf("typeName %v\n", typeName)

var g interface{}
g = f
typeName = reflect.TypeOf(g).Name()
fmt.Printf("typeName %v\n", typeName)

var h interface{}
h = &f
typeName = reflect.TypeOf(h).Name()
fmt.Printf("typeName %v\n", typeName)
}

Outputs:



typeName Foo
typeName Foo
typeName

Also at:


http://ift.tt/1xnyTMA






Aucun commentaire:

Enregistrer un commentaire