init
This commit is contained in:
58
pkg/util/strUtil/strUtil.go
Normal file
58
pkg/util/strUtil/strUtil.go
Normal file
@ -0,0 +1,58 @@
|
||||
package strUtil
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// JoinStrings 字符串拼接
|
||||
func JoinStrings(separator string, elems ...string) string {
|
||||
return strings.Join(elems, separator)
|
||||
}
|
||||
|
||||
// ValidString sql.NullString 转 string
|
||||
func ValidString(sqlString sql.NullString) string {
|
||||
if sqlString.Valid {
|
||||
return sqlString.String
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// StringToNullString string 转 sql.NullString
|
||||
func StringToNullString(str string) sql.NullString {
|
||||
return sql.NullString{
|
||||
String: str,
|
||||
Valid: str != "",
|
||||
}
|
||||
}
|
||||
|
||||
// ValidInt64 sql.NullInt64 转 int64
|
||||
func ValidInt64(sqlInt sql.NullInt64) int64 {
|
||||
if sqlInt.Valid {
|
||||
return sqlInt.Int64
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
// Int64ToNullInt64 StringToNullString int64 转 sql.NullInt64
|
||||
func Int64ToNullInt64(value int64) sql.NullInt64 {
|
||||
return sql.NullInt64{
|
||||
Int64: value,
|
||||
Valid: value >= 0,
|
||||
}
|
||||
}
|
||||
|
||||
// ValidBool sql.NullBool 转 bool
|
||||
func ValidBool(sqlBool sql.NullBool) bool {
|
||||
if sqlBool.Valid {
|
||||
return sqlBool.Bool
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// MatchRegexp 正则匹配
|
||||
func MatchRegexp(pattern string, value string) bool {
|
||||
r := regexp.MustCompile(pattern)
|
||||
return r.MatchString(value)
|
||||
}
|
Reference in New Issue
Block a user