simplify the code

This commit is contained in:
dablelv 2021-01-01 16:03:28 +08:00
parent 4b4f3c28f6
commit 97cf7b2944
1 changed files with 8 additions and 7 deletions

View File

@ -849,19 +849,20 @@ func ToStringE(i interface{}) (string, error) {
}
// support type redefinition
switch reflect.TypeOf(i).Kind() {
v := reflect.ValueOf(i)
switch v.Kind() {
case reflect.String:
return reflect.ValueOf(i).String(), nil
return v.String(), nil
case reflect.Bool:
return strconv.FormatBool(reflect.ValueOf(i).Bool()), nil
return strconv.FormatBool(v.Bool()), nil
case reflect.Float32:
return strconv.FormatFloat(reflect.ValueOf(i).Float(), 'f', -1, 32), nil
return strconv.FormatFloat(v.Float(), 'f', -1, 32), nil
case reflect.Float64:
return strconv.FormatFloat(reflect.ValueOf(i).Float(), 'f', -1, 64), nil
return strconv.FormatFloat(v.Float(), 'f', -1, 64), nil
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return strconv.FormatInt(reflect.ValueOf(i).Int(), 10), nil
return strconv.FormatInt(v.Int(), 10), nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return strconv.FormatUint(reflect.ValueOf(i).Uint(), 10), nil
return strconv.FormatUint(v.Uint(), 10), nil
}
return "", fmt.Errorf("unable to cast %#v of type %T to string", i, i)
}