From 97cf7b2944eedb4ea6440fc1b000118e4fbba7c2 Mon Sep 17 00:00:00 2001 From: dablelv Date: Fri, 1 Jan 2021 16:03:28 +0800 Subject: [PATCH] simplify the code --- caste.go | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/caste.go b/caste.go index 718f3dd..04caf94 100644 --- a/caste.go +++ b/caste.go @@ -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) }