dimanche 10 décembre 2017

Go type switch with `time.Time`

In my test I have a function that can get a value from a struct like this:

func getField(v interface{}, field string) string {
    r := reflect.ValueOf(v)
    f := reflect.Indirect(r).FieldByName(field)
    t := f.Kind()

    switch t {
    case reflect.Int, reflect.Int64:
        return strconv.FormatInt(f.Int(), 10)
    case reflect.String:
        return f.String()
    case reflect.Bool:
        if f.Bool() {
            return "true"
        }
        return "false"
    return ""
}

This works for the types above but I can't get it to work for time.Time. How do I do this?





Aucun commentaire:

Enregistrer un commentaire