vendredi 18 mars 2022

Calling NumField on Value panics in some cases (go)

I'm using reflect.ValueOf(..) to loop through elements in a struct. I noticed that calling NumField fails if i pass the struct vs. pointer to the struct in the ValueOf function.

v = reflect.ValueOf(user)
v.NumField() // panics

Vs.

v = reflect.ValueOf(*user)
v.NumField() // works

Is there a way to find out beforehand if v would panic, before calling NumField?





Aucun commentaire:

Enregistrer un commentaire