util

package module
v0.0.0-...-62caf1b Latest Latest
Warning

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

Go to latest
Published: Dec 25, 2025 License: GPL-3.0 Imports: 25 Imported by: 0

README

util

import

go get github.com/13inary/util

Documentation

Index

Constants

View Source
const (
	// 避免不关注年的时间变量出现分钟的差错
	// 1901年前使用LMT(分钟会有误差),1928年后国际天文学会正式确立时区标准UTC,1949年后新中国时区统一CST
	NotLMTYear = 1928
)

Variables

This section is empty.

Functions

func AtomicWriteSmallFile

func AtomicWriteSmallFile(file string, content []byte, perm os.FileMode) error

异步写文件。写完文件并关闭后,若在操作系统同步文件前断电,会丢失文件。

func BytesToFloat32

func BytesToFloat32(b []byte, order binary.ByteOrder) float32

func ConvertModel

func ConvertModel(srcModel any, dstModelPointer any) error

ConvertModel 源模型的字段会覆盖目标模型的字段。注意转换多个源到目的模型时多个源的字段相同问题。

func CutRightZeroBytes

func CutRightZeroBytes(b []byte) []byte

CutRightZeroBytes 一旦发现0x00值,立即切掉后面所有数据

func DateOnlyInt2Time

func DateOnlyInt2Time(dateInt int) time.Time

输入数字:20250101 输出:time.Time 性能比数字转字符串再转time.Time快很多

func FileExists

func FileExists(path string) (bool, error)

FileExists 检查指定路径的文件是否存在 参数:

path - 要检查的文件路径

返回值:

bool - 文件存在返回true,否则false
error - 底层系统调用返回的错误(当错误不是文件不存在时)

func Float2String

func Float2String(f float64, suffix string) string

func Float32ToBytes

func Float32ToBytes(f float32, order binary.ByteOrder) []byte

func Float64Invalid

func Float64Invalid(f float64) bool

math相关函数的报错,通过返回结果代替error

func GBKToUTF8

func GBKToUTF8(gbk []byte) ([]byte, error)

GBKToUTF8 将GBK编码字节切片转换为UTF-8编码字节切片

func GenRandomMil

func GenRandomMil(maxMill int64) time.Duration

func GroupSort

func GroupSort[T any](src []T, getGroupKey func(T) string, sortGroup func([]T, []T) bool, sortMember func(T, T) bool) []T

GroupSort 对切片进行分组排序处理,没必要排序的元素会保持原顺序 参数:

src - 需要处理的源切片
getGroupKey - 获取元素分组键的函数,返回字符串类型的分组标识
sortGroup - 组间排序函数,参数是两个组的元素切片,返回true表示第一个组应排在第二个组前,为nil时不排序
sortMember - 组内排序函数,用于对同一组的元素进行排序,返回true表示第一个元素应排在第二个元素前,为nil时不排序

返回值:

[]T - 返回按分组排序后的新切片

注意:

排序函数返回false都会触发元素更换位置,也就是大小比较用好等于号
sortGroup有2个slice传进来,它们的长度>=1

func HasPrefixs

func HasPrefixs(str string, prefixs []string) bool

func HasSuffixs

func HasSuffixs(str string, suffixs []string) bool

func HtmlGB180302UTF8

func HtmlGB180302UTF8(body io.ReadCloser) io.Reader

func HtmlGBK2UTF8

func HtmlGBK2UTF8(body io.ReadCloser) io.Reader

func HtmlWindows12522UTF8

func HtmlWindows12522UTF8(body io.ReadCloser) io.Reader

func HttpGetContent

func HttpGetContent(fullUrl string, headers map[string]string, params map[string]string) ([]byte, error)

HttpGetContent 提供获取http内容的能力

func HttpGetHtml

func HttpGetHtml(fullUrl string, headers map[string]string, params map[string]string, coder func(body io.ReadCloser) io.Reader) (string, error)

HttpGetHtml 提供获取html的能力

func HttpPost

func HttpPost(fullUrl string, headers map[string]string, formDatas map[string]string) ([]byte, error)

HttpPost 提供post请求的能力

func InitTimezone

func InitTimezone()

时间问题1:CST,处理新旧时区体系导致的误差 时间问题2:8*3600,不同地域时区导致的误差

func Int2String

func Int2String(i int, suffix string) string

func IsIPv4

func IsIPv4(s string) bool

func LoadFromCache

func LoadFromCache[T any](cacheFile string) (T, error)

缓存是否过期,由外部的函数判断

func LoadFromCsv

func LoadFromCsv(csvFile string) ([][]string, error)

func LoopExecute

func LoopExecute(ctx context.Context, job func() error, prepare time.Duration, interval time.Duration) error

LoopExecute 2次逻辑执行相隔的时间为:逻辑的执行时间 + 间隔时间

func Map2Struct

func Map2Struct(srcMap map[string]any, dstStructPointer any) error

Map2Struct 对于结构体字段名,若map中有就拿过来覆盖。 对于嵌套结构体的问题:调用方传入嵌套的字段。

func NewPrintCharPositionDiff

func NewPrintCharPositionDiff() *printDiff

func RemoveAllZeroBytes

func RemoveAllZeroBytes(b []byte) []byte

RemoveAllZeroBytes 移除字节切片中的所有0x00值

func SaveToCache

func SaveToCache[T any](data T, cacheFile string) error

func SaveToCsv

func SaveToCsv(data [][]string, csvFile string, readOnly bool) error

func StrTerminalLen

func StrTerminalLen(str string) int

StrTerminalLen 获取字符串在终端中输出的长度 例子: 中文 => 4 en => 2

func SyncAtomicWriteSmallFile

func SyncAtomicWriteSmallFile(file string, content []byte, perm os.FileMode) error

同步写文件。写完文件后,让操作系统同步文件,然后再关闭文件,避免文件的丢失。 同步操作会损耗性能

func TableStr

func TableStr(lines [][]string, colMaxWidth []int) string

TableStr 获取可以展示表格的字符串 将多个line[]string,加入lines[][]string中 当 colMaxWidth 为nil,花费时间来自动计算

func TextBlue

func TextBlue(src string) string

func TextGreen

func TextGreen(src string) string

func TextPurple

func TextPurple(src string) string

func TextRed

func TextRed(src string) string

func TextYellow

func TextYellow(src string) string

func TrimZeroBytes

func TrimZeroBytes(b []byte) []byte

TrimZeroBytes 移除字节切片首尾的0x00值

func TryDo

func TryDo(ctx context.Context, job func() (bool, error), maxTimes int) error

func UTF8ToGBK

func UTF8ToGBK(utf8 []byte) ([]byte, error)

UTF8ToGBK 将UTF-8编码字节切片转换为GBK编码字节切片

Types

This section is empty.

Jump to

Keyboard shortcuts

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