mercredi 4 mars 2020

Reflect function eliminate mixing of switch and if statement in a function

This question is mainly just about cosmetics, this code does it's tented job, but I would love to be able to use the switch statement in both cases instead of mixing if and switch commands.

func fieldValue(field reflect.Value) string {
    format := "%v"
    if stringer, ok := field.Interface().(strfmt.DateTime); ok {
        return fmt.Sprintf(format, stringer.String())
    }
    switch field.Kind() {
    case reflect.Ptr:
        if field.IsNil() {
            return "<nil>"
        }
        field = field.Elem()
    }
    return fmt.Sprintf(format, field.Interface())
}




Aucun commentaire:

Enregistrer un commentaire