字符串转int无默认值版

This commit is contained in:
xing 2023-02-21 20:31:00 +08:00
parent b588b7e6d4
commit df11d6522d

View File

@ -32,6 +32,17 @@ func ToInteger[T constraints.Integer](s string, defaults T) T {
} }
return T(i) return T(i)
} }
func ToInt[T constraints.Integer](s string) T {
defaults := T(0)
if s == "" {
return defaults
}
i, err := strconv.ParseInt(s, 10, 64)
if err != nil {
return defaults
}
return T(i)
}
func Md5(str string) string { func Md5(str string) string {
h := md5.New() h := md5.New()