Categorygithub.com/xiaoyang-chen/utils-golang
modulepackage
0.0.0-20240522224756-f978f095d5af
Repository: https://github.com/xiaoyang-chen/utils-golang.git
Documentation: pkg.go.dev

# README

utils-golang

reduce times of writing same function

# Functions

BigInt2Bytes 将b转换成byte-slice, 当 b == nil 时返回nil, 当b的数值为0时, 返回空slice(非nil), 假设a为b转换成的byte-slice, 当 b > 0 时返回的slice为'+' + a, 当 b < 0 时返回的slice为'-' + a.
BigIntAbs returns |x|, if x == nil, will panic.
BigIntCmp compares a and b and returns: -1 if a < b 0 if a == b +1 if a > b if a == nil || b == nil, will panic.
BigIntCopy returns copy of src, if src == nil, will panic.
BigIntEqual a和是否相等, if a == nil || b == nil, will panic.
BigIntGt returns true if a > b, if a == nil || b == nil, will panic.
BigIntGte returns true if a >= b, if a == nil || b == nil, will panic.
BigIntIsZero 判断b是否为0, 当b为nil时返回true.
BigIntLt returns true if a < b, if a == nil || b == nil, will panic.
BigIntLte returns true if a <= b, if a == nil || b == nil, will panic.
BigIntMax returns max(a, b), if a == b, return a, if a == nil || b == nil, will panic.
BigIntMin returns min(a, b), if a == b, return a, if a == nil || b == nil, will panic.
BigIntNeg returns -x, if x == nil, will panic.
No description provided by the author
Bytes2Str 将bytes通过复用底层指针的方式转换成string, 转换后的string会 跟随用于转换的bytes里数据改变而改变, 所以转换后, 原先的bytes最好不再使用, 或者只用只读的方式 */.
CheckCNPhonesFormat phones 多个手机号用','隔开.
Float64ToKMGT 大于1000时默认输出2位小数(小于1000时输出所有小数忽略末尾的0), 单位为K(kilo),M(Mega),G(Giga),T(Tera)的数值, 1K == 10^3, 1M == 10^6, 1G == 10^9, 1T == 10^12.
GetLikeSqlParamStrInTwoPercent 将 likeStr: "1d2fg3" 转化为 sqlParam: "'%1d2fg3%'", likeStr == ""或likeStr含有'''(单引号) => sqlParam: "", 返回值 sqlParam 可以直接用于sql语句中, 不会被sql注入.
GetMulQuestionMarkDividedByComma 返回用逗号隔开的length个'?', length <= 0 => sqlParam: "", length == 1 => sqlParam: "?", length > 1 => sqlParam: "?,...,?".
GetSqlParamFromIntegers 将 integers: [123 23 22] 转化为 sqlParam: "123,23,22", 当integers不为整数类型的slice时sqlParam为"", 返回值 sqlParam 可以直接用于sql语句中, 不会被sql注入.
GetSqlParamFromStrings 将 strings: [123asd a23fc c2rda2] 转化为 sqlParam: '123asd', 'a23fc', 'c2rda2', len(strings) == 0 => sqlParam: "", strings里的各个字符串不能存在字符'''(单引号), '[', ']', ' '(空格符), 返回值 sqlParam 可以直接用于sql语句中, 不会被sql注入.
GetSqlParamStrInTwoSingleQuote 将 srcStr: "1d2fg3" 转化为 sqlParam: "'1d2fg3'", srcStr == "" => sqlParam: "''", srcStr含有'''(单引号) => sqlParam: "", 返回值 sqlParam 可以直接用于sql语句中, 不会被sql注入.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
IsOnlyHasDigitalAndComma 判断字符串是否只有数字和逗号, 字符串为空或只有数字和逗号时返回true, 其他为false.
No description provided by the author
No description provided by the author
NewBigIntFromBytesWithNeg 将buf里的数据解析成b(*big.int), 当buf为空时, 返回0对应的*big.int, isNeg: 解析后的b是否需要转换为相反数(b = -b), 默认为false.
NewBigIntFromString 根据十进制对str进行解析.
NewBigIntMustFromString if parse str fail, will panic.
No description provided by the author
NextTime returns the next time base on spec(linux cron), start from param start.
NumToFloat64 将数值转换成float64,为数值返回浮点数值和true,非数值返回0和false.
No description provided by the author
No description provided by the author
RunWithRecover 返回.
Str2Bytes 把 string 零成本转换为 []byte 没有多余的内存开销。 对转换后的[]byte添加字符, 都会生成一个新的[]byte即一定触发扩容。 这个函数可以提升 string 转 []byte 的性能,并极大的降低内存开销, 但是却相当的危险,对于不明确这个函数的人来说不建议使用该函数。 该函数使用不当会导致程序直接崩溃,且无法恢复。 s := `{"k":"v"}` b := Str2Bytes(s) // b[3] = 'k' // unexpected fault address 0x1118180 data := map[string]string{} err := json.Unmarshal(b, &data) fmt.Println(data, err) 这是一个使用的例子,如果我们需要转换一个字符串很方便,且开销非常的低。 但是一定要注意,b[3] = 'k' 如果尝试修改获得的 []byte 将直接导致程序崩溃,并且不可能通过 recover() 恢复。 实际上我们可以突破这个限制,这就要了解字符串的一些规则, 下面的例子可以完美运行,并修改字符串: s := strings.Repeat("A", 3) b := exstrings.UnsafeToBytes(s) b[1] = 'B' b[2] = 'C' fmt.Println(s, string(b)) 非常完美,s和b变量的值都是 ABC, 为什么会这样呢? 这个就是 string 的内存分配方法, 字面量使用这种方式是没有办法修改的, 因为这是在编译时就决定的,编译时会设定字符串的内存数据是只读数据。 如果程序运行时生成的数据,这种数据是可以安全使用该函数的, 但是要当心你的字符串可能会被修改, 比如我们调用 json.Unmarshal(Str2Bytes(s), &data), 如果 json 包里面出现修改输入参数,那么原来的字符串就可能不是你想想的那样。 使用该函数要明确两个事情: - 确定字符串是否是字面量,或者内存被分配在只读空间上。 - 确保访问该函数结果的函数是否会按照你的意愿访问或修改数据。 虽然它很危险,但是有时间却很有用,如果我们需要大批量转换字符串的大小写, 而且不再需要原字符串,我们可以原地安全的修改字符串。 当然还有更多的使用方法,可以极大的提升我们程序的性能。 */.
No description provided by the author
No description provided by the author
TickerRunWithContext will block current goroutine, usage: go TickerRunWithContext(...).
TimestampToLocalTime 将10/13/16/19位时间戳转换成本地时间字符串, 小于10位返回"timestamp wrong", 10: s级时间戳,13: ms级时间戳,16: us级时间戳,19: ns级时间戳, 10位格式为2006-01-02 15:04:05, 13位格式为2006-01-02 15:04:05.000, 16位格式为2006-01-02 15:04:05.000000, 19位格式为2006-01-02 15:04:05.000000000.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
UnixCmdResLinesWithTimeout only get stdout, timeout is valid when timeout > 0.
UnixCmdSyncBytes only get stdout.
UnixCmdSyncString only get stdout.

# Variables

No description provided by the author

# Interfaces

No description provided by the author

# Type aliases

JsonRawMessage is a raw encoded JSON value, copy from encoding/json RawMessage.