初始版本

This commit is contained in:
2026-05-06 14:53:45 +08:00
commit b759ceb1c6
10 changed files with 814 additions and 0 deletions

43
convx/convx.go Normal file
View File

@@ -0,0 +1,43 @@
package convx
import (
"encoding/json"
"strconv"
)
func Int(v any) int {
return int(Int64(v))
}
func Int64(v any) int64 {
switch n := v.(type) {
case int:
return int64(n)
case int64:
return n
case float64:
return int64(n)
case json.Number:
i, _ := n.Int64()
return i
case string:
i, _ := strconv.ParseInt(n, 10, 64)
return i
default:
return 0
}
}
func ValueOrZero(v, invalid int) int {
if v == invalid {
return 0
}
return v
}
func Ternary[T any](cond bool, yes, no T) T {
if cond {
return yes
}
return no
}