10

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://play.golang.org/p/2QuBoDxHfX

1 Answer 1

8

As the Name method's documentation says, unnamed types will return an empty string:

Name returns the type's name within its package. It returns an empty string for unnamed types.

The type of h is an unnamed pointer type whose element type is the named struct type Foo:

v := reflect.TypeOf(h)
fmt.Println(v.Elem().Name()) // prints "Foo"

If you want an identifier for complex unnamed types like this, use the String method:

fmt.Println(v.String()) // prints "*main.Foo"
4
  • Thanks!. Really interesting. Reading the docs on Elem() is a bit confusing. // Elem returns a type's element type. // It panics if the type's Kind is not Array, Chan, Map, Ptr, or Slice. Elem() Type. So, reflect.TypeOf returned a Type, Elem returns an elements Type. Is the "element" the value that I'm reflecting on?
    – Nate
    Feb 5, 2015 at 7:57
  • Also, it seems that the only reason Elem() isn't panic'ing is because the contained type is a pointer. I find it interesting that Elem().Name() then returns "Foo" instead of *Foo.
    – Nate
    Feb 5, 2015 at 8:03
  • 2
    @Nate: The name Elem is a bit missleading for pointers but it is perfectly sensible for the other types. E.g. if you reflect on a []int the Elem() method gives you the type of the slice elements, here int (and not []int). The same logic applies to pointers:You reflect on a pointer type and Elem() gives you the type of the "element of the pointer type" and thats Name is clearly "Foo" not "*Foo" as the name of the element of a []int is int and not []int.
    – Volker
    Feb 5, 2015 at 8:19
  • For what it is worth, the type of h is interface{} not a pointer type. It is the value of h that has (unnamed) pointer type. This becomes apparent if you pass in &h to reflect.TypeOf instead of h. However, interfaces are passed by value (mostly) in Go, and reflect.TypeOf takes interface{} so it has no way of knowing the type of h if passed by value. This distinction is not super important here, but would be more noticeable if h had a named interface type like io.Reader.
    – kbolino
    Aug 31, 2018 at 0:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.