utils

package
v0.0.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 28, 2025 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const (
	LicenseLoginFileName = "/etc/service/license/license_login.conf" // 正式版本路径
	UptimeFile           = "/proc/uptime"                            //linux
	//////////
	POST    = "POST"
	GET     = "GET"
	HEAD    = "HEAD"
	PUT     = "PUT"
	DELETE  = "DELETE"
	PATCH   = "PATCH"
	OPTIONS = "OPTIONS"
)

Variables

View Source
var (
	// int 强制转换
	Int   = InterfaceToInt
	Int64 = InterfaceToInt64
	Int32 = InterfaceToInt32
	// float 强制转换
	Float64 = InterfaceToFloat64
	// string 强制转换
	Str = InterfaceToStr
	// bool 强制转换
	Bool = InterfaceToBool
)
View Source
var TFormat = "2006-01-02" // 默认时间戳转字符串格式
View Source
var TimeFormat = "2006-01-02 15:04:05" // 默认时间戳转字符串格式
View Source
var TimeFormatRexMap = map[string]string{
	`^[+|-]\d{4}\s\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}$`: "-0700 2006-01-02 15:04:05",
	`^\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}$`:             "2006-01-02 15:04:05",
	`^\d{4}/\d{2}/\d{2}\s\d{2}:\d{2}:\d{2}[+|-]\d{4}$`:   "2006/01/02 15:04:05-0700",
	`^\d{4}/\d{2}/\d{2}T\d{2}:\d{2}:\d{2}[+|-]\d{4}$`:    "2006/01/02T15:04:05-0700",
	`^\d{4}/\d{2}/\d{2}\s\d{2}:\d{2}:\d{2}$`:             "2006/01/02 15:04:05",
	`^\d{4}/\d{2}/\d{2}T\d{2}:\d{2}:\d{2}$`:              "2006/01/02T15:04:05",

} // 字符串转时间戳匹配模式

Functions

func BJsonToListMap

func BJsonToListMap(bdata []byte) (jmap []map[string]interface{})

将切片类型的json流byte 转换成map切片

func BJsonToMap

func BJsonToMap(bdata []byte) (jmap map[string]interface{})

将json流 转换成map

func Bind

func Bind(data interface{}, ret interface{}) error

data 转换成ret

func CeilTo added in v0.0.5

func CeilTo(x float64, n int) float64

CeilTo 向上取整到 n 位小数(支持 n 为负数)

func CheckIp

func CheckIp(ipAddr string) bool

func EndWith

func EndWith(value, str string) bool

func FileOpenRead

func FileOpenRead(filename string) (contents []uint8, err error)

open and read

func Float64ToStr

func Float64ToStr(obj float64) (sli string)

将切片 interface转成切片字符串

func FloorTo added in v0.0.5

func FloorTo(x float64, n int) float64

FloorTo 向下取整到 n 位小数(支持 n 为负数)

func Formattimezone

func Formattimezone() string

调用系统命令返回内容 返回运行系统时区

func FromTimeStamp

func FromTimeStamp(in interface{}) string

func GetYesterdayTime

func GetYesterdayTime() []string

获取昨天0点起止时间

func If

func If(b bool, to, fo interface{}) interface{}

3目运算

func InToMap

func InToMap(b []interface{}) []map[string]interface{}

[]interface to []map[string]interface{}

func IntToStr

func IntToStr(in int) string

func InterToSliceString

func InterToSliceString(obj interface{}) []string

将切片 interface转成切片字符串

func InterfaceToBool

func InterfaceToBool(x interface{}) bool

func InterfaceToFloat64

func InterfaceToFloat64(x interface{}) float64

func InterfaceToInt

func InterfaceToInt(x interface{}) int

func InterfaceToInt32

func InterfaceToInt32(x interface{}) int32

func InterfaceToInt64

func InterfaceToInt64(x interface{}) int64

强制转化int64

func InterfaceToStr

func InterfaceToStr(x interface{}) string

func IsLeapYear

func IsLeapYear(year int) bool

判断是否为闰年

func IterToMap

func IterToMap(obj interface{}) map[string]interface{}

func IterToMaps

func IterToMaps(obj interface{}) []map[string]interface{}

func JoinRedisKey

func JoinRedisKey(key ...string) string

func JsonNumberToFloat64

func JsonNumberToFloat64(obj json.Number) float64

json.Number to float64

func JsonNumberToInt64

func JsonNumberToInt64(obj json.Number) int64

json.Number to int64 json.Number转换int64

func LastDuration

func LastDuration() (sTime, eTime string)

当前小时上一个小时时间段计算

func ListMaxVal

func ListMaxVal(list []int) (maxval int)

func ListMaxValInt

func ListMaxValInt(slice interface{}) (maxIndex, minIndex int, maxVal, minVal, avgVal float64)

获取一个数组里最大值,并且拿到下标

func ListStructToListMap

func ListStructToListMap(obj []interface{}) []map[string]interface{}

[]struct to []map[string]interface{}

func MapArrToObjArr

func MapArrToObjArr(old []map[string]interface{}) []interface{}

map数组转成对象数组

func MapToBuffer

func MapToBuffer(m interface{}) *bytes.Buffer

将map/struct 转成buffer流

func MapToStr

func MapToStr(param map[string]interface{}) string

map转str

func MapToString

func MapToString(m interface{}) string

map 转换成map字符串(jsonz字符串)

func MatToBuffer

func MatToBuffer(m map[string]interface{}) *bytes.Buffer

将map转化成字节流

func MathCeil

func MathCeil(x float64) int

向上取整

func MathFloor

func MathFloor(x float64) int

向下取整

func PathExists

func PathExists(path string) (bool, error)

判断文件是否存在

func Pretty

func Pretty(data interface{}) string

json 格式返回查看数据

func RangeKeyListMap

func RangeKeyListMap(obj interface{}, list []map[string]interface{}) bool

判断map切片是否存在一个key

func RangeListMap

func RangeListMap(obj map[string]interface{}, list []map[string]interface{}) bool

range []map[string]interface{}判断map是否存在一个列表中

func RangeMap

func RangeMap(obj interface{}, list map[string]interface{}) bool

判断map是否存在一个key

func Rangelist

func Rangelist(b interface{}, list []interface{}) bool

遍历轮询切片

func ReadFile

func ReadFile(filename string) (string, error)

ReadFile 读取文件内容

func Readlines

func Readlines(filename string) (readlineslist []string, err error)

readlines 读取全部行 返回列表

func RemoveDuplicatesAndEmpty

func RemoveDuplicatesAndEmpty(a []string) (ret []string)

数组去重

func RemoveRepeatedElement

func RemoveRepeatedElement(arr []string) (newArr []string)

去重列表内容

func RemoveRepeatedElementList

func RemoveRepeatedElementList(arr []interface{}) (newArr []interface{})

去重列表内容

func Round added in v0.0.5

func Round(x float64, n int) float64

Round 保留 n 位小数(四舍五入,5 进,远离 0;支持 n 为负数)

func RoundEven added in v0.0.5

func RoundEven(x float64, n int) float64

RoundEven 银行家舍入:四舍六入,五看前一位,前一位为偶则舍弃(ties-to-even)

func SJsonToListMap

func SJsonToListMap(sdata string) (jmap []map[string]interface{})

将切片json流字符串 转换成map切片

func SJsonToMap

func SJsonToMap(sdata string) (jmap map[string]interface{})

将json流字符串 转换成map

func Set

func Set(in interface{}, out interface{})

简单的set方法

func SetListString

func SetListString(list1, list2 []string) []string

对两个切片字符串去重并相减

func SetListStringadd

func SetListStringadd(list1, list2 []string) (listAdd []string)

对两个切片字符串去重并合并

func SliceInterToString

func SliceInterToString(obj []interface{}) (sli []string)

将切片 interface转成切片字符串

func SliceToStr

func SliceToStr(v interface{}) string

func SortData

func SortData(data interface{}, sortkey string, reverse bool)

根据指定字符排序

m := []map[string]int{
	{"k": 2},
	{"k": 1},
	{"k": 3},
}

customer.SortData(&m, "k", true) ture 倒序3, 2, 1 fmt.Println(m)

func SortDataEx

func SortDataEx(data interface{}, sortkey []string, reverse bool)

func SortedMap

func SortedMap(mapslice []map[string]interface{}, sortkey string, direction bool) []map[string]interface{}

对切片map排序 冒泡排序 mapslice 需要排序的map类型的切片 ,sortkey,排序key键关键字 , direction true为递增排序false为递减排序

func StartWith

func StartWith(value, str string) bool

func StrToInt

func StrToInt(in string) int

func StrToMap

func StrToMap(str string) map[string]interface{}

str转map

func StringToFloat64

func StringToFloat64(obj string) float64

string to float64

func StringToInt

func StringToInt(obj string) int

string to int

func StringToInt64

func StringToInt64(obj string) int64

string to int64

func StringToMap

func StringToMap(str string) (jmap map[string]interface{})

将string转换成map

func StringToSlice

func StringToSlice(s string) []string

func StructToMap

func StructToMap(obj interface{}) map[string]interface{}

struct 转map

func StructToMapMore

func StructToMapMore(obj interface{}) map[string]interface{}

struct 转map

func StructToMapMore1

func StructToMapMore1(obj interface{}) map[string][]map[string]interface{}

struct 转map

func ToFixed added in v0.0.5

func ToFixed(x float64, n int) string

ToFixed 将数值格式化为固定 n 位小数字符串(不改变数值,适合展示)

func ToInt

func ToInt(x interface{}) int

func ToSlice

func ToSlice(arr interface{}) []interface{}

interface to map interface

func ToString

func ToString(i interface{}) string

类型转换

func ToTimeStamp

func ToTimeStamp(in string) int64

根据指定时间格式返回时间戳

func Trunc added in v0.0.5

func Trunc(x float64, n int) float64

Trunc 保留 n 位小数(直接截断,不四舍五入;支持 n 为负数)

func UnixToTime

func UnixToTime(t, typ int64) (sTime string)

根据时间戳获取时间

func VerifyEmailFormat

func VerifyEmailFormat(email string) bool

验证邮箱

func WriteFile

func WriteFile(filename string, data []byte) error

WriteFile 将内容写入文件

Types

type Cloud_env_Get

type Cloud_env_Get struct {
	Ip   string `json:"ip"`
	Port string `json:"port"`
}

获取公共平台访问地址、端口类型设定

type SaveDebug

type SaveDebug struct {
	UserID   string      `json:"user_id" bson:"user_id" form:"user_id" query:"user_id"  validate:"user_id"`
	UserRole string      `json:"user_role" bson:"user_role" form:"user_role" query:"user_role"  validate:"user_role"`
	ClientIp string      `json:"client_ip" bson:"client_ip" form:"client_ip" query:"client_ip"  validate:"client_ip"`
	Optype   string      `json:"optype" bson:"optype" form:"optype" query:"optype"  validate:"optype"`
	Content  interface{} `json:"content" bson:"content" form:"content" query:"content"  validate:"content"`
	Ret      interface{} `json:"ret" bson:"ret" form:"ret" query:"ret"  validate:"ret"`
	Time     int         `json:"time" bson:"time" form:"time" query:"time"  validate:"time"`
	ErrorMsg string      `json:"error_msg" bson:"error_msg" form:"error_msg" query:"error_msg"  validate:"error_msg"`
}

全局

type SendData

type SendData struct {
	Success bool        `json:"success" bson:"success"`
	Ret     interface{} `json:"ret" bson:"ret"`
}

全局通用 返回请求数据strcut

type SortBy

type SortBy struct {
	Data    []map[string]interface{}
	Sortkey string
}

排序 排序键必须为数字类型

func (SortBy) Len

func (a SortBy) Len() int

func (SortBy) Less

func (a SortBy) Less(i, j int) bool

func (SortBy) Swap

func (a SortBy) Swap(i, j int)

type SortByEx

type SortByEx struct {
	Data    []map[string]interface{}
	Sortkey []string
}

排序(slow)

func (SortByEx) Len

func (a SortByEx) Len() int

func (SortByEx) Less

func (a SortByEx) Less(i, j int) bool

func (SortByEx) LessSub

func (a SortByEx) LessSub(keyindex, i, j int) bool

func (SortByEx) Swap

func (a SortByEx) Swap(i, j int)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL