lundi 10 août 2015

Get fields of empty struct slice in Go

I have a function

func (r *render) foo(v interface{}) {
    val := reflect.ValueOf(v)
    fields := structs.Fields(val.Index(0).Interface())

...

Which takes a slice of structs and tries to get the fields of v, however if v is empty then "val.Index(0)" crashes the program. Is there a better way to do this?





Aucun commentaire:

Enregistrer un commentaire