1 Star 0 Fork 0

carlmax_my/console-core-go

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
trans.go 2.14 KB
一键复制 编辑 原始数据 按行查看 历史
carlmax_my 提交于 2024-12-02 21:32 . init project
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
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/carlmax_my/console-core-go.git
git@gitee.com:carlmax_my/console-core-go.git
carlmax_my
console-core-go
console-core-go
v0.0.19

搜索帮助

0d507c66 1850385 C8b1a773 1850385