代码拉取完成,页面将自动刷新
package util
import (
"encoding/json"
"strconv"
)
func String(s string) *string {
return &s
}
func BToU(b bool) uint8 {
if b {
return 1
}
return 0
}
// interface{} -> int
func InterfaceToInt(t1 any) int {
var t2 int
switch t1 := t1.(type) {
case uint:
t2 = int(t1)
case int8:
t2 = int(t1)
case uint8:
t2 = int(t1)
case int16:
t2 = int(t1)
case uint16:
t2 = int(t1)
case int32:
t2 = int(t1)
case uint32:
t2 = int(t1)
case int64:
t2 = int(t1)
case uint64:
t2 = int(t1)
case float32:
t2 = int(t1)
case float64:
t2 = int(t1)
case string:
t2, _ = strconv.Atoi(t1)
default:
//t2 = t1.(int)
t2 = 0
}
return t2
}
// interface{} -> int64
func InterfaceToInt64(t1 any) int64 {
var t2 int64
switch t1 := t1.(type) {
case int:
t2 = int64(t1)
case uint:
t2 = int64(t1)
case int32:
t2 = int64(t1)
case uint32:
t2 = int64(t1)
case int64:
t2 = int64(t1)
case uint64:
t2 = int64(t1)
case float32:
t2 = int64(t1)
case float64:
t2 = int64(t1)
case string:
t2, _ = strconv.ParseInt(t1, 10, 64)
default:
//t2 = t1.(int64)
t2 = 0
}
return t2
}
// interface{} -> uint64
func InterfaceToUInt64(t1 any) uint64 {
var t2 uint64
switch t1 := t1.(type) {
case int64:
t2 = uint64(t1)
case uint64:
t2 = t1
case float32:
t2 = uint64(t1)
case float64:
t2 = uint64(t1)
case string:
x, _ := strconv.ParseInt(t1, 10, 64)
t2 = uint64(x)
default:
//t2 = t1.(uint64)
t2 = 0
}
return t2
}
// interface{} -> float64
func InterfaceToFloat64(t1 any) float64 {
var t2 float64
switch t1 := t1.(type) {
case int64:
t2 = float64(t1)
case uint64:
t2 = float64(t1)
case float32:
t2 = float64(t1)
case float64:
t2 = t1
case string:
t2, _ = strconv.ParseFloat(t1, 64)
default:
//t2 = t1.(float64)
t2 = 0
}
return t2
}
func InterfaceToString(t1 any) string {
var t2 string
switch t1 := t1.(type) {
case string:
t2 = t1
}
return t2
}
func InterfaceToStruct[T any](a any) (*T, error) {
resByre, resByteErr := json.Marshal(a)
if resByteErr != nil {
return nil, resByteErr
}
t := new(T)
jsonRes := json.Unmarshal(resByre, t)
if jsonRes != nil {
return nil, jsonRes
}
return t, nil
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。