优化ToString
This commit is contained in:
+68
-33
@@ -6,39 +6,74 @@ import (
|
||||
)
|
||||
|
||||
// interface 转 string
|
||||
func ToString(val interface{}) (str string, err error) {
|
||||
// func ToString(val interface{}) (str string, err error) {
|
||||
|
||||
var s string
|
||||
if vv, ok := val.(float32); ok {
|
||||
s = strconv.FormatFloat(float64(vv), 'f', -1, 32)
|
||||
} else if vv, ok := val.(float64); ok {
|
||||
s = strconv.FormatFloat(vv, 'f', -1, 64)
|
||||
} else if vv, ok := val.(int); ok {
|
||||
s = strconv.Itoa(vv)
|
||||
} else if vv, ok := val.(int32); ok {
|
||||
s = strconv.Itoa(int(vv))
|
||||
} else if vv, ok := val.(int64); ok {
|
||||
s = strconv.FormatInt(vv, 10)
|
||||
} else if vv, ok := val.(string); ok {
|
||||
s = vv
|
||||
} else if vv, ok := val.(bool); ok {
|
||||
s = strconv.FormatBool(vv)
|
||||
} else if vv, ok := val.(uint); ok {
|
||||
s = strconv.FormatUint(uint64(vv), 10)
|
||||
} else if vv, ok := val.(uint32); ok {
|
||||
s = strconv.FormatUint(uint64(vv), 10)
|
||||
} else if vv, ok := val.(uint64); ok {
|
||||
s = strconv.FormatUint(vv, 10)
|
||||
} else if vv, ok := val.(uint8); ok {
|
||||
s = strconv.FormatUint(uint64(vv), 10)
|
||||
} else if vv, ok := val.(uint16); ok {
|
||||
s = strconv.FormatUint(uint64(vv), 10)
|
||||
} else if vv, ok := val.(int8); ok {
|
||||
s = strconv.FormatInt(int64(vv), 10)
|
||||
} else if vv, ok := val.(int16); ok {
|
||||
s = strconv.FormatInt(int64(vv), 10)
|
||||
} else {
|
||||
return s, errors.New("不支持的参数类型")
|
||||
// var s string
|
||||
// if vv, ok := val.(float32); ok {
|
||||
// s = strconv.FormatFloat(float64(vv), 'f', -1, 32)
|
||||
// } else if vv, ok := val.(float64); ok {
|
||||
// s = strconv.FormatFloat(vv, 'f', -1, 64)
|
||||
// } else if vv, ok := val.(int); ok {
|
||||
// s = strconv.Itoa(vv)
|
||||
// } else if vv, ok := val.(int32); ok {
|
||||
// s = strconv.Itoa(int(vv))
|
||||
// } else if vv, ok := val.(int64); ok {
|
||||
// s = strconv.FormatInt(vv, 10)
|
||||
// } else if vv, ok := val.(string); ok {
|
||||
// s = vv
|
||||
// } else if vv, ok := val.(bool); ok {
|
||||
// s = strconv.FormatBool(vv)
|
||||
// } else if vv, ok := val.(uint); ok {
|
||||
// s = strconv.FormatUint(uint64(vv), 10)
|
||||
// } else if vv, ok := val.(uint32); ok {
|
||||
// s = strconv.FormatUint(uint64(vv), 10)
|
||||
// } else if vv, ok := val.(uint64); ok {
|
||||
// s = strconv.FormatUint(vv, 10)
|
||||
// } else if vv, ok := val.(uint8); ok {
|
||||
// s = strconv.FormatUint(uint64(vv), 10)
|
||||
// } else if vv, ok := val.(uint16); ok {
|
||||
// s = strconv.FormatUint(uint64(vv), 10)
|
||||
// } else if vv, ok := val.(int8); ok {
|
||||
// s = strconv.FormatInt(int64(vv), 10)
|
||||
// } else if vv, ok := val.(int16); ok {
|
||||
// s = strconv.FormatInt(int64(vv), 10)
|
||||
// } else {
|
||||
// return s, errors.New("不支持的参数类型")
|
||||
// }
|
||||
// return s, nil
|
||||
// }
|
||||
|
||||
func ToString(val interface{}) (string, error) {
|
||||
switch v := val.(type) {
|
||||
case float32:
|
||||
return strconv.FormatFloat(float64(v), 'f', -1, 32), nil
|
||||
case float64:
|
||||
return strconv.FormatFloat(v, 'f', -1, 64), nil
|
||||
case int:
|
||||
return strconv.Itoa(v), nil
|
||||
case int32:
|
||||
return strconv.Itoa(int(v)), nil
|
||||
case int64:
|
||||
return strconv.FormatInt(v, 10), nil
|
||||
case string:
|
||||
return v, nil
|
||||
case bool:
|
||||
return strconv.FormatBool(v), nil
|
||||
case uint:
|
||||
return strconv.FormatUint(uint64(v), 10), nil
|
||||
case uint32:
|
||||
return strconv.FormatUint(uint64(v), 10), nil
|
||||
case uint64:
|
||||
return strconv.FormatUint(v, 10), nil
|
||||
case uint8:
|
||||
return strconv.FormatUint(uint64(v), 10), nil
|
||||
case uint16:
|
||||
return strconv.FormatUint(uint64(v), 10), nil
|
||||
case int8:
|
||||
return strconv.FormatInt(int64(v), 10), nil
|
||||
case int16:
|
||||
return strconv.FormatInt(int64(v), 10), nil
|
||||
default:
|
||||
return fmt.Sprintf("%v", val), nil
|
||||
}
|
||||
return s, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user