Commit 3e92c9d9 by zhengqiuyun86

nil默认

parent cca51f65
...@@ -8,5 +8,8 @@ func If[K any](isTrue bool, a, b K) K { ...@@ -8,5 +8,8 @@ func If[K any](isTrue bool, a, b K) K {
} }
func IfNull[K any](a *K, defaultValue K) K { func IfNull[K any](a *K, defaultValue K) K {
return If(a == nil, defaultValue, *a) if a == nil {
return defaultValue
}
return *a
} }
...@@ -11,10 +11,15 @@ func TestContainsForSplitComma(t *testing.T) { ...@@ -11,10 +11,15 @@ func TestContainsForSplitComma(t *testing.T) {
fmt.Printf("%s是否包含在[%s]中:%v", a, b, ContainsForSplitComma(b, a)) fmt.Printf("%s是否包含在[%s]中:%v", a, b, ContainsForSplitComma(b, a))
} }
type DefaultCount struct {
Count *int32 `gorm:"column:count" json:"count"` // 笔数
Money *float64 `gorm:"column:money" json:"money"` // 金额(元)
}
func TestStrval(t *testing.T) { func TestStrval(t *testing.T) {
fmt.Printf("转换内容:%s", GetInterfaceToString(3.10)) fmt.Printf("转换内容:%s", GetInterfaceToString(3.10))
var a int var count DefaultCount
a = If(true, 12, 0) //a := If(count.Count == nil, 12, 0)
a = IfNull(&a, 0) a := IfNull(count.Count, 0)
fmt.Printf("结果:%d", a) fmt.Printf("结果:%d", a)
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment