refactor: code review

This commit is contained in:
meilin.huang
2023-10-20 21:31:46 +08:00
parent 45d2449221
commit 10f6b03fb5
15 changed files with 270 additions and 209 deletions

View File

@@ -2,8 +2,6 @@ package stringx
import (
"bytes"
"encoding/json"
"strconv"
"strings"
"text/template"
)
@@ -117,45 +115,3 @@ func ReverStrTemplate(temp, str string, res map[string]any) {
ReverStrTemplate(next, Trim(SubString(str, UnicodeIndex(str, value)+Len(value), Len(str))), res)
}
}
func AnyToStr(value any) string {
// interface 转 string
var key string
if value == nil {
return key
}
switch it := value.(type) {
case string:
return it
case float64:
return strconv.FormatFloat(it, 'f', -1, 64)
case float32:
return strconv.FormatFloat(float64(it), 'f', -1, 64)
case int:
return strconv.Itoa(it)
case uint:
return strconv.Itoa(int(it))
case int8:
return strconv.Itoa(int(it))
case uint8:
return strconv.Itoa(int(it))
case int16:
return strconv.Itoa(int(it))
case uint16:
return strconv.Itoa(int(it))
case int32:
return strconv.Itoa(int(it))
case uint32:
return strconv.Itoa(int(it))
case int64:
return strconv.FormatInt(it, 10)
case uint64:
return strconv.FormatUint(it, 10)
case []byte:
return string(value.([]byte))
default:
newValue, _ := json.Marshal(value)
return string(newValue)
}
}