mardi 29 juin 2021

Call MethodByName in array of struct: panic: reflect: call of reflect.Value.Call on zero Value

I am trying to use reflection to make a dynamic function call of each element of an array:

var EsPersonList []EsEntry

func (this *EsEntry) FullName() string {
    return this.Wholename
}

func createStr(d interface{}) {
    items := reflect.ValueOf(d)

    if items.Kind() == reflect.Slice {
        for i := 0; i < items.Len(); i++ {
            item := items.Index(i)
            if item.Kind() == reflect.Struct {
                v := reflect.ValueOf(&item)
                return_values := v.MethodByName("FullName").Call([]reflect.Value{})
                fmt.Println(return_values)
            }
        }
    }
}

createStr(EsPersonList)

I get is a panic that looks like this: panic: reflect: call of reflect.Value.Call on zero Value

https://play.golang.org/p/vK2hUfVcMwr

How can I fix this?





Aucun commentaire:

Enregistrer un commentaire