I have a piece of reflection code that attempts to get the field on a struct by name and then checks if the field exists:
type test struct {
A bool
B bool
}
t := new(test)
metaValue := reflect.ValueOf(t)
field := metaValue.FieldByName(name)
if field.IsZero() {
glog.Errorf("Field %s was not on the struct", inner)
}
According to the documentation on FieldByName
, this function should return a zero value if no field was found. However, the very next line panics with the error:
panic: reflect: call of reflect.Value.IsZero on zero Value
goroutine 268 [running]:
reflect.Value.IsZero({0x0, 0x0, 0x112a974})
reflect/value.go:1475 +0x27f
According to this GitHub issue, this should only happen if the Value contains nil (i.e. no type) and IsValid
should be used instead. Why is this happening?
Aucun commentaire:
Enregistrer un commentaire