utils

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 5, 2024 License: MIT Imports: 33 Imported by: 0

Documentation

Index

Constants

View Source
const (
	PrecModeDecimalPlace = 2
	PrecModeSignifDigits = 3
	PrecModeTickSize     = 4
)
View Source
const (
	UriEncodeSafe = "~()*!.'"
)

Variables

View Source
var (
	ErrInvalidPrecision   = errors.New("invalid precision")
	ErrNegPrecForTickSize = errors.New("negative precision for tick size")
)

Functions

func ArrContains

func ArrContains[T comparable](s []T, e T) bool

func ByteToStruct

func ByteToStruct[T any](byteChan <-chan []byte, outChan chan<- T)

ByteToStruct 将[]byte类型的chan通道,转为指定类型通道

func DecToPrec

func DecToPrec(num string, countMode int, precision string, isRound, padZero bool) (string, error)

DecToPrec 将数字转换为指定精度返回

num: 需要处理的数字字符串
countMode: PrecModeDecimalPlace 保留小数点后指定位;
PrecModeSignifDigits 保留全部有效数字个数
PrecModeTickSize 返回给定的数字的整数倍
precision: 结合countMode使用,正整数,负整数,正浮点数
isRound: 是否四舍五入,默认截断
padZero: 是否尾部填充0到指定长度

func Eddsa

func Eddsa(request []byte, secret []byte) (string, error)

func EncodeToLatin1

func EncodeToLatin1(input string) ([]byte, error)

func EncodeURIComponent

func EncodeURIComponent(str string, safe string) string

func EqualIn

func EqualIn(a, b, thres float64) bool

EqualIn 判断两个float是否在一定范围内近似相等

func EqualNearly

func EqualNearly(a, b float64) bool

EqualNearly 判断两个float是否近似相等,解决浮点精读导致不等

func GetMapFloat

func GetMapFloat(data map[string]interface{}, key string) float64

func GetMapVal

func GetMapVal[T any](items map[string]interface{}, key string, defVal T) T

GetMapVal 从map中获取指定类型的值。只支持简单类型,不支持slice,map,array,struct等

func HMAC

func HMAC(request []byte, secret []byte, algorithm func() hash.Hash, digest string) string

func ISO8601

func ISO8601(millis int64) string

func IsNil

func IsNil(i interface{}) bool

IsNil 判断是否为nil

golang中类型和值是分开存储的,如果一个指针有类型,值为nil,直接判断==nil会返回false

func MapValStr

func MapValStr(input map[string]interface{}) map[string]string

func OmitMapKeys

func OmitMapKeys(items map[string]interface{}, keys ...string)

func ParseTimeFrame

func ParseTimeFrame(timeframe string) (int, *errs.Error)

func PopMapVal

func PopMapVal[T any](items map[string]interface{}, key string, defVal T) T

PopMapVal 从map中获取指定类型的值并删除。只支持简单类型,不支持slice,map,array,struct等

func PrecFloat64

func PrecFloat64(num float64, prec float64, isRound bool) (float64, error)

PrecFloat64 对给定浮点数取近似值,精确到指定位数

func PrecFloat64Str

func PrecFloat64Str(num float64, prec float64, isRound bool) (string, error)

func PrecisionFromString

func PrecisionFromString(input string) float64

func ReadFile

func ReadFile(path string) ([]byte, error)

func ReadJsonFile

func ReadJsonFile(path string, obj interface{}) error

func SafeMapVal

func SafeMapVal[T any](items map[string]string, key string, defVal T) (result T, err error)

SafeMapVal 从字典中读取给定键的值,并自动转换为需要的类型,如果出错则返回默认值

func SafeParams

func SafeParams(params map[string]interface{}) map[string]interface{}

func SetFieldBy

func SetFieldBy[T any](field *T, items map[string]interface{}, key string, defVal T)

func Signature

func Signature(data string, secret string, method string, hashName string, digest string) (string, *errs.Error)

Signature method: rsa, eddsa, hmac

hashName:
	rsa: sha256/sha384/sha512
	eddsa: 不需要
	hmac:sha256/sha384/sha512
digest: hmac: base64/hex

func UUID

func UUID(length int) string

func Unmarshal added in v0.2.0

func Unmarshal(data []byte, out interface{}) error

func UnmarshalString added in v0.2.0

func UnmarshalString(text string, out interface{}) error

UnmarshalString 替代sonic.UnmarshalString,默认函数在将int64的长整数反序列化时,转为float64,导致精度损失,这里强制使用int64解码

func UrlEncodeMap

func UrlEncodeMap(params map[string]interface{}, escape bool) string

UrlEncodeMap 将map编码为url查询字符串 escape: 是否对键和值进行编码

func WriteFile

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

func WriteJsonFile

func WriteJsonFile(path string, data interface{}) error

func YMD

func YMD(timestamp int64, separator string, fullYear bool) string

YMD 将13位时间戳转为日期

separator 年月日的间隔字符 fullYear 年是否使用4个数字

Types

This section is empty.

Jump to

Keyboard shortcuts

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