1 Star 0 Fork 0

h79/goutils

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
utils.go 4.62 KB
一键复制 编辑 原始数据 按行查看 历史
huqiuyun 提交于 2年前 . db 完善 cond
package common
import (
"fmt"
"reflect"
"strconv"
"strings"
)
func ArrayToInt(arr []interface{}) []int {
Int := make([]int, len(arr))
for i := range arr {
Int[i] = int(ToInt64(arr[i]))
}
return Int
}
func ToInt64(value interface{}) int64 {
reflectValue := reflect.Indirect(reflect.ValueOf(value))
return ToInt64V2(reflectValue)
}
func ToInt64V2(reflectValue reflect.Value) int64 {
switch reflectValue.Kind() {
case reflect.String:
if i, err := strconv.ParseInt(reflectValue.String(), 10, 64); err == nil {
return i
}
return 0
case reflect.Int:
fallthrough
case reflect.Int8:
fallthrough
case reflect.Int16:
fallthrough
case reflect.Int32:
fallthrough
case reflect.Int64:
return reflectValue.Int()
}
return 0
}
func ToFloat64(value interface{}) float64 {
reflectValue := reflect.Indirect(reflect.ValueOf(value))
return ToFloat64V2(reflectValue)
}
func ToFloat64V2(reflectValue reflect.Value) float64 {
switch reflectValue.Kind() {
case reflect.String:
f, _ := strconv.ParseFloat(reflectValue.String(), 4)
return f
case reflect.Int:
fallthrough
case reflect.Int8:
fallthrough
case reflect.Int16:
fallthrough
case reflect.Int32:
fallthrough
case reflect.Int64:
return float64(reflectValue.Int())
case reflect.Float32:
fallthrough
case reflect.Float64:
return reflectValue.Float()
}
return 0.0
}
func ToUInt64(value interface{}) uint64 {
reflectValue := reflect.Indirect(reflect.ValueOf(value))
return ToUInt64V2(reflectValue)
}
func ToUInt64V2(reflectValue reflect.Value) uint64 {
switch reflectValue.Kind() {
case reflect.String:
if i, err := strconv.ParseUint(reflectValue.String(), 10, 64); err == nil {
return i
}
return 0
case reflect.Uint:
fallthrough
case reflect.Uint8:
fallthrough
case reflect.Uint16:
fallthrough
case reflect.Uint32:
fallthrough
case reflect.Uint64:
return reflectValue.Uint()
}
return 0
}
func ToString(value interface{}) string {
reflectValue := reflect.Indirect(reflect.ValueOf(value))
return ToStringV2(reflectValue)
}
func ToStringV2(reflectValue reflect.Value) string {
switch reflectValue.Kind() {
case reflect.String:
return reflectValue.String()
case reflect.Int:
fallthrough
case reflect.Int8:
fallthrough
case reflect.Int16:
fallthrough
case reflect.Int32:
fallthrough
case reflect.Int64:
return strconv.FormatInt(reflectValue.Int(), 10)
case reflect.Uint:
fallthrough
case reflect.Uint8:
fallthrough
case reflect.Uint16:
fallthrough
case reflect.Uint32:
fallthrough
case reflect.Uint64:
return strconv.FormatUint(reflectValue.Uint(), 10)
case reflect.Float32:
fallthrough
case reflect.Float64:
return strconv.FormatFloat(reflectValue.Float(), 'f', 5, 64)
case reflect.Bool:
if reflectValue.Bool() {
return "true"
} else {
return "false"
}
default:
msg := fmt.Sprintf("not support the '%v' data", reflectValue.Kind())
panic(msg)
}
return ""
}
func IsNil(o interface{}) bool {
if o == nil {
return true
}
val := reflect.ValueOf(o)
switch val.Kind() {
case reflect.Pointer, reflect.UnsafePointer:
fallthrough
case reflect.Chan, reflect.Func, reflect.Map:
fallthrough
case reflect.Interface, reflect.Slice:
return val.IsNil()
}
return false
}
// IsMap checks if some variable is a map
func IsMap(o interface{}) bool {
if o == nil {
return false
}
return reflect.ValueOf(o).Kind() == reflect.Map
}
// IsSlice checks if some variable is a slice
func IsSlice(o interface{}) bool {
if o == nil {
return false
}
return reflect.ValueOf(o).Kind() == reflect.Slice
}
func StrctVal(s interface{}) reflect.Value {
v := reflect.ValueOf(s)
// if pointer get the underlying element≤
for v.Kind() == reflect.Ptr {
v = v.Elem()
}
if v.Kind() != reflect.Struct {
panic("not struct")
}
return v
}
func StructFields(value reflect.Value, tagName string) []reflect.StructField {
t := value.Type()
var f []reflect.StructField
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
// we can't access the value of unexported fields
if field.PkgPath != "" {
continue
}
// don't check if it's omitted
if tag := field.Tag.Get(tagName); tag == "-" {
continue
}
f = append(f, field)
}
return f
}
// tagOptions contains a slice of tag options
type tagOptions []string
// Has returns true if the given option is available in tagOptions
func (t tagOptions) Has(opt string) bool {
for _, tagOpt := range t {
if tagOpt == opt {
return true
}
}
return false
}
func ParseTag(tag string) (string, tagOptions) {
// tag is one of followings:
// ""
// "name"
// "name,opt"
// "name,opt,opt2"
// ",opt"
res := strings.Split(tag, ",")
return res[0], res[1:]
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/h79/goutils.git
git@gitee.com:h79/goutils.git
h79
goutils
goutils
v1.2.21

搜索帮助