Files
hldrCenter/server/util/utiltrans.go

46 lines
1.1 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package util
import "database/sql"
func Int64ToInt(ni int64) int {
return int(ni) // 直接强转,无需处理空值(基础类型非空)
}
// NullStringToString 转换 sql.NullString 到 string
// 若字段无效Valid=false返回空字符串
func NullStringToString(ns sql.NullString) string {
if ns.Valid {
return ns.String
}
return ""
}
func StringToNullString(s string) sql.NullString {
if s != "" {
return sql.NullString{String: s, Valid: true}
}
return sql.NullString{Valid: false}
}
// NullInt64ToInt 转换 sql.NullInt64 到 int
// 若字段无效Valid=false返回0可根据业务调整默认值
func NullInt64ToInt(ni sql.NullInt64) int {
if ni.Valid {
return int(ni.Int64)
}
return 0
}
// NullTimeToString 转换 sql.NullTime 到格式化字符串
// 若字段无效,返回空字符串;格式默认为 "2006-01-02 15:04:05"
func NullTimeToString(nt sql.NullTime, format ...string) string {
if !nt.Valid {
return ""
}
f := "2006-01-02 15:04:05"
if len(format) > 0 {
f = format[0]
}
return nt.Time.Format(f)
}