Get all fields from an interface

RAFJR picture RAFJR · Oct 5, 2016 · Viewed 15.8k times · Source

How do I know the fields I can access from the reply object/interface? I tried reflection but it seems you have to know the field name first. What if I need to know all the fields available to me?

// Do sends a command to the server and returns the received reply.
Do(commandName string, args ...interface{}) (reply interface{}, err error)

Answer

icza picture icza · Oct 5, 2016

You can use the reflect.TypeOf() function to obtain a reflect.Type type descriptor. From there, you can list fields of the dynamic value stored in the interface.

Example:

type Point struct {
    X int
    Y int
}

var reply interface{} = Point{1, 2}
t := reflect.TypeOf(reply)
for i := 0; i < t.NumField(); i++ {
    fmt.Printf("%+v\n", t.Field(i))
}

Output:

{Name:X PkgPath: Type:int Tag: Offset:0 Index:[0] Anonymous:false}
{Name:Y PkgPath: Type:int Tag: Offset:4 Index:[1] Anonymous:false}

The result of a Type.Field() call is a reflect.StructField value which is a struct, containing the name of the field among other things:

type StructField struct {
    // Name is the field name.
    Name string
    // ...
}

If you also want the values of the fields, you may use reflect.ValueOf() to obtain a reflect.Value(), and then you may use Value.Field() or Value.FieldByName():

v := reflect.ValueOf(reply)
for i := 0; i < v.NumField(); i++ {
    fmt.Println(v.Field(i))
}

Output:

1
2

Try it on the Go Playground.

Note: often a pointer to struct is wrapped in an interface. In such cases you may use Type.Elem() and Value.Elem() to "navigate" to the pointed type or value:

t := reflect.TypeOf(reply).Elem()

v := reflect.ValueOf(reply).Elem()

If you don't know whether it's a pointer or not, you can check it with Type.Kind() and Value.Kind(), comparing the result with reflect.Ptr:

t := reflect.TypeOf(reply)
if t.Kind() == reflect.Ptr {
    t = t.Elem()
}

// ...

v := reflect.ValueOf(reply)
if v.Kind() == reflect.Ptr {
    v = v.Elem()
}

Try this variant on the Go Playground.

For a detailed introduction to Go's reflection, read the blog post: The Laws of Reflection.