gutil

package module
v0.7.6 Latest Latest
Warning

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

Go to latest
Published: Mar 20, 2024 License: GPL-3.0 Imports: 33 Imported by: 0

README

gutil

go 语言统一工具类

Installation

When used with Go modules, use the following import path:

go get github.com/HC74/gutil@latest

工具详情

字符串

StringIsEmpty
描述

判断字符串是否为空。

参数
  • str: 要判断的字符串。
返回值

布尔值,如果为空就返回true。

示例

import (
	"github.com/HC74/gutil"
)
str := ""
isnull := gutil.StringIsEmpty(str) // true

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	TimeLayoutDate     = "2006-01-02"
	TimeLayoutDateTime = "2006-01-02 15:04:05"
)

Functions

func BcryptCheck

func BcryptCheck(password, hash string) bool

BcryptCheck Bcrypt解密 @params password : 被解密对象

func BcryptEncrypt

func BcryptEncrypt(password string) string

BcryptEncrypt Bcrypt加密 @params password : 被加密对象

func Build

func Build[T any](s *SqlBuilder, v *T) (int64, int64, error)

Build 构建 r1 总页数 r2 总数据量 r3 恐慌

func BuildNotPage

func BuildNotPage[T any](s *SqlBuilder, v *T) error

BuildNotPage 构建无需page信息 @returns 恐慌

func BytesToInt64

func BytesToInt64(b []byte) (tmp int64)

BytesToInt64 []byte转换int64

func BytesTouInt64

func BytesTouInt64(b []byte) (tmp uint64)

BytesTouInt64 []byte转换uint64

func BytesTouInt8

func BytesTouInt8(b []byte) (tmp uint8)

BytesTouInt8 []byte转换uint8

func Contains added in v0.7.2

func Contains(slice []string, element string) bool

Contains 判断切片中是否包含某元素

func DataSlice added in v0.7.2

func DataSlice[T any](v []T, size int) [][]T

DataSlice 数据分片

func ExtractTime added in v0.7.6

func ExtractTime(id int64) time.Time

func FileExists

func FileExists(file string) bool

func FileIsDir

func FileIsDir(file string) bool

func GenerateRandomString

func GenerateRandomString(n int) string

GenerateRandomString 生成定长的随机数

func GetBody

func GetBody[T any](url string, headers M) (*T, error)

GetBody 泛型Get

func GetPost

func GetPost[T any](url string, headers M, body interface{}) (*T, error)

GetPost 泛型post

func GrepFile

func GrepFile(patten string, filename string) (lines []string, err error)

GrepFile like command grep -E for example: GrepFile(`^hello`, "hello.txt") \n is striped while read

func GuidID

func GuidID() string

func Int64ToBytes

func Int64ToBytes(n int64) (b []byte)

Int64ToBytes int64转[]byte

func IsRegularFile

func IsRegularFile(fi os.FileInfo) bool

func IsValidTime

func IsValidTime(t time.Time) bool

func Md5 added in v0.7.2

func Md5(str string) string

Md5 MD5

func MonthStart

func MonthStart() time.Time

func NextId added in v0.7.6

func NextId() int64

NextId .

func Now

func Now() string

Now 获取当前时间 默认为 YYYY-MM-dd HH:ss:mm 格式

func NowDate

func NowDate() string

func NowDateTime

func NowDateTime() string

func NowUnix

func NowUnix() int64

func PKCS5Padding added in v0.7.2

func PKCS5Padding(data []byte, blockSize int) []byte

PKCS5Padding 对数据进行填充

func PKCS5UnPadding added in v0.7.2

func PKCS5UnPadding(data []byte) []byte

PKCS5UnPadding 去除填充数据

func ParseDate

func ParseDate(dt string) (time.Time, error)

func ParseDateTime

func ParseDateTime(dt string) (time.Time, error)

func ParseStringTime

func ParseStringTime(tm, lc string) (time.Time, error)

func RGetFieldsValues

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

RGetFieldsValues 获取给定结构体的所有字段名和字段值

func RGetTypeName

func RGetTypeName(value interface{}) string

RGetTypeName 获取给定值的类型名称

func RIsEmpty

func RIsEmpty(value interface{}) bool

RIsEmpty 判断给定值是否为空

func RIsPointer

func RIsPointer(value interface{}) bool

RIsPointer 判断给定值是否为指针类型

func ReadFile

func ReadFile(filename string) (string, error)

func SearchFile

func SearchFile(filename string, paths ...string) (fullpath string, err error)

SearchFile Search a file in paths. this is often used in search config file in /etc ~/

func SecureAESDecrypt added in v0.7.2

func SecureAESDecrypt(key, ciphertext []byte) ([]byte, error)

SecureAESDecrypt ASE解密

func SecureAESEncrypt added in v0.7.2

func SecureAESEncrypt(key, text []byte) ([]byte, error)

SecureAESEncrypt ASE加密

func SecureDESDecrypt added in v0.7.2

func SecureDESDecrypt(ciphertext, key []byte) ([]byte, error)

SecureDESDecrypt DES解密

func SecureDESEncrypt added in v0.7.2

func SecureDESEncrypt(plaintext, key []byte) ([]byte, error)

SecureDESEncrypt DES加密

func SelfDir

func SelfDir() string

SelfDir gets compiled executable file directory

func SelfPath

func SelfPath() string

SelfPath gets compiled executable file absolute path

func SetIdGenerator added in v0.7.6

func SetIdGenerator(options *IdGeneratorOptions)

SetIdGenerator .

func SinceForHuman

func SinceForHuman(t time.Time) string

SinceForHuman 1小时前 -> 这样的展示方式

func SnowInit added in v0.7.6

func SnowInit(workerId uint16)

func StringGetHash added in v0.7.2

func StringGetHash(s string) string

StringGetHash 计算字符串的哈希值

func StringIsEmpty

func StringIsEmpty(v string) bool

StringIsEmpty 判断字符串是否为空

func StringRandom added in v0.7.2

func StringRandom(n int) string

StringRandom 生成随机字符串

func Tar

func Tar(src, dest string) (err error)

func TarRecursive

func TarRecursive(src, dest string) (err error)

func ToSqlCondition added in v0.7.3

func ToSqlCondition(tableName string, v interface{}) map[string]interface{}

ToSqlCondition 转换为SQL条件

func ToUnifiedTime

func ToUnifiedTime(v time.Time) string

ToUnifiedTime 统一返回 YYYY-MM-dd HH:ss:mm

func TodayEnd

func TodayEnd() time.Time

func TodayStart

func TodayStart() time.Time

func Uint64ToBytes

func Uint64ToBytes(n uint64) (b []byte)

Uint64ToBytes uint64转[]byte

func UnTar

func UnTar(src, dest string) (err error)

UnTar 解压文件. src is the abbreviation of source, which is a directory of files to be decompressed. dst is the abbreviation of destination, which is the decompressed file directory.

func UnZip

func UnZip(dst, src string) (err error)

UnZip 解压缩文件.

func Zip

func Zip(src, dest string) (err error)

Zip 压缩成Zip

func ZipRecursive

func ZipRecursive(src, dest string) (err error)

ZipRecursive Zip Compressed file.

Types

type DefaultIdGenerator added in v0.7.6

type DefaultIdGenerator struct {
	Options    *IdGeneratorOptions // ID 生成器的选项
	SnowWorker ISnowWorker         // Snowflake 算法的工作器
}

DefaultIdGenerator 是默认的 ID 生成器实现。

func NewDefaultIdGenerator added in v0.7.6

func NewDefaultIdGenerator(options *IdGeneratorOptions) *DefaultIdGenerator

NewDefaultIdGenerator 创建一个新的默认 ID 生成器。

func (DefaultIdGenerator) ExtractTime added in v0.7.6

func (dig DefaultIdGenerator) ExtractTime(id int64) time.Time

ExtractTime 从 ID 中提取时间信息。

func (DefaultIdGenerator) NewLong added in v0.7.6

func (dig DefaultIdGenerator) NewLong() int64

NewLong 生成一个长整型的 ID。

type FileChunk

type FileChunk struct {
	Number int   // Chunk number
	Offset int64 // Chunk offset
	Size   int64 // Chunk size.
}

func SplitFileByPartNum

func SplitFileByPartNum(fileSize int64, chunkNum int) ([]FileChunk, error)

SplitFileByPartNum 按分块数分块

func SplitFileByPartSize

func SplitFileByPartSize(fileSize int64, chunkSize int64) ([]FileChunk, error)

SplitFileByPartSize 按大小分块, 参考阿里云

type HeapTimeout added in v0.7.6

type HeapTimeout []Node[T]

func (HeapTimeout) Len added in v0.7.6

func (ht HeapTimeout) Len() int

func (HeapTimeout) Less added in v0.7.6

func (ht HeapTimeout) Less(i, j int) bool

func (HeapTimeout) Push added in v0.7.6

func (ht HeapTimeout) Push(x any)

func (HeapTimeout) Swap added in v0.7.6

func (ht HeapTimeout) Swap(i, j int)

type HttpClient

type HttpClient struct {
	// contains filtered or unexported fields
}

func NewHttpClient

func NewHttpClient() *HttpClient

NewHttpClient 创建httpClient请求实例

func (*HttpClient) GetBody

func (c *HttpClient) GetBody(url string, headers M, data interface{}) error

GetBody 发送get请求并完成数据获取

func (*HttpClient) GetBodyN

func (c *HttpClient) GetBodyN(url string, data interface{}) error

GetBodyN 发送get请求并完成数据获取

func (*HttpClient) GetString

func (c *HttpClient) GetString(url string, headers M) (string, error)

GetString 发送get请求且包含请求头

func (*HttpClient) GetStringN

func (c *HttpClient) GetStringN(url string) (string, error)

GetStringN 发送get请求且不包含请求头

func (*HttpClient) PostBody

func (c *HttpClient) PostBody(url string, headers M, body interface{}, data interface{}) error

PostBody 发送post请求并完成数据获取

func (*HttpClient) PostBodyN

func (c *HttpClient) PostBodyN(url string, body interface{}, data interface{}) error

PostBodyN 发送post请求并不包含请求头且完成数据获取

func (*HttpClient) PostString

func (c *HttpClient) PostString(url string, headers M, body interface{}) (string, error)

PostString 发送post请求返回string

func (*HttpClient) PostStringN

func (c *HttpClient) PostStringN(url string, body interface{}) (string, error)

PostStringN 发送post请求且不包含请求头

type ISnowWorker added in v0.7.6

type ISnowWorker interface {
	NextId() int64
}

func NewSnowWorkerDefault added in v0.7.6

func NewSnowWorkerDefault(options *IdGeneratorOptions) ISnowWorker

NewSnowWorkerDefault 创建一个新的 Snowflake 算法的工作器。

type IdGeneratorOptions added in v0.7.6

type IdGeneratorOptions struct {
	BaseTime          int64  // 基础时间(ms单位),不能超过当前系统时间
	WorkerId          uint16 // 机器码,必须由外部设定,最大值 2^WorkerIdBitLength-1
	WorkerIdBitLength byte   // 机器码位长,默认值6,取值范围 [1, 15](要求:序列数位长+机器码位长不超过22)
	SeqBitLength      byte   // 序列数位长,默认值6,取值范围 [3, 21](要求:序列数位长+机器码位长不超过22)
	MaxSeqNumber      uint32 // 最大序列数(含),设置范围 [MinSeqNumber, 2^SeqBitLength-1],默认值0,表示最大序列数取最大值(2^SeqBitLength-1])
	MinSeqNumber      uint32 // 最小序列数(含),默认值5,取值范围 [5, MaxSeqNumber],每毫秒的前5个序列数对应编号0-4是保留位,其中1-4是时间回拨相应预留位,0是手工新值预留位
	TopOverCostCount  uint32 // 最大漂移次数(含),默认2000,推荐范围500-10000(与计算能力有关)
}

func NewIdGeneratorOptions added in v0.7.6

func NewIdGeneratorOptions(workerId uint16) *IdGeneratorOptions

type M

type M = map[string]string

type Node added in v0.7.6

type Node[T any] struct {
	Deadline int64
	Key      T
}

type ObjectId

type ObjectId string

type OrmBuilder added in v0.7.3

type OrmBuilder struct {
	TableName string
	// contains filtered or unexported fields
}

func Orm added in v0.7.3

func Orm(db *gorm.DB, tableName string) *OrmBuilder

func (*OrmBuilder) DB added in v0.7.3

func (o *OrmBuilder) DB() *gorm.DB

DB 获取DB实例

func (*OrmBuilder) Find added in v0.7.3

func (o *OrmBuilder) Find(v interface{}) error

Find 查询同Gorm的Find

func (*OrmBuilder) Fusion added in v0.7.3

func (o *OrmBuilder) Fusion(v interface{}) *OrmBuilder

Fusion 将条件和DB融合为Orm操作实例

type Ring added in v0.7.6

type Ring[T any] struct {
	Value T // for use by client; untouched by this library
	// contains filtered or unexported fields
}

func NewRing added in v0.7.6

func NewRing[T any](n int) *Ring[T]

NewRing creates a ring of n elements

func (*Ring[T]) Do added in v0.7.6

func (r *Ring[T]) Do(fn func(t T))

func (*Ring[T]) Len added in v0.7.6

func (r *Ring[T]) Len() int

Len computes the number of elements in ring r. It executes in time proportional to the number of elements.

func (r *Ring[T]) Link(s *Ring[T]) *Ring[T]

Link connects ring r with ring s such that r.Next() becomes s and returns the original value for r.Next(). r must not be empty.

If r and s point to the same ring, linking them removes the elements between r and s from the ring. The removed elements form a subring and the result is a reference to that subring (if no elements were removed, the result is still the original value for r.Next(), and not nil).

If r and s point to different rings, linking them creates a single ring with the elements of s inserted after r. The result points to the element following the last element of s after insertion.

func (*Ring[T]) Move added in v0.7.6

func (r *Ring[T]) Move(n int) *Ring[T]

Move moves n % r.Len() elements backward (n < 0) or forward (n >= 0) in the ring and returns that ring element. r must not be empty.

func (*Ring[T]) Next added in v0.7.6

func (r *Ring[T]) Next() *Ring[T]

Next returns the next ring element. r must not be empty.

func (*Ring[T]) Prev added in v0.7.6

func (r *Ring[T]) Prev() *Ring[T]

Prev returns the previous ring element. r must not be empty.

func (r *Ring[T]) Unlink(n int) *Ring[T]

Unlink removes n % r.Len() elements from the ring r, starting at r.Next(). If n % r.Len() == 0, r remains unchanged. The result is the removed subring. r must not be empty.

type SnowWorkerDefault added in v0.7.6

type SnowWorkerDefault struct {
	BaseTime          int64  // 基础时间
	WorkerId          uint16 // 机器码
	WorkerIdBitLength byte   // 机器码位长
	SeqBitLength      byte   // 自增序列数位长
	MaxSeqNumber      uint32 // 最大序列数(含)
	MinSeqNumber      uint32 // 最小序列数(含)
	TopOverCostCount  uint32 // 最大漂移次数

	sync.Mutex
	// contains filtered or unexported fields
}

SnowWorkerDefault 实现了基于 Snowflake 算法的工作器。

func (*SnowWorkerDefault) CalcId added in v0.7.6

func (m1 *SnowWorkerDefault) CalcId(useTimeTick int64) int64

CalcId 计算给定时间戳的 ID。

func (*SnowWorkerDefault) CalcTurnBackId added in v0.7.6

func (m1 *SnowWorkerDefault) CalcTurnBackId(useTimeTick int64) int64

CalcTurnBackId 计算时间回拨时的 ID。

func (*SnowWorkerDefault) GetCurrentTimeTick added in v0.7.6

func (m1 *SnowWorkerDefault) GetCurrentTimeTick() int64

GetCurrentTimeTick .

func (*SnowWorkerDefault) GetNextTimeTick added in v0.7.6

func (m1 *SnowWorkerDefault) GetNextTimeTick() int64

GetNextTimeTick .

func (*SnowWorkerDefault) NextId added in v0.7.6

func (m1 *SnowWorkerDefault) NextId() int64

NextId .

func (*SnowWorkerDefault) NextNormalId added in v0.7.6

func (m1 *SnowWorkerDefault) NextNormalId() int64

NextNormalId 生成下一个正常的 ID。

func (*SnowWorkerDefault) NextOverCostId added in v0.7.6

func (m1 *SnowWorkerDefault) NextOverCostId() int64

NextOverCostId 生成下一个超出成本的 ID。

type SqlBuilder

type SqlBuilder struct {
	BaseSql   string
	Temp      string
	Condition map[string]interface{}
	Total     int64

	DB *gorm.DB
	// contains filtered or unexported fields
}

func SqlSelect

func SqlSelect(db *gorm.DB, s ...string) *SqlBuilder

SqlSelect 创建sql合并

func (*SqlBuilder) Count

func (s *SqlBuilder) Count() int64

func (*SqlBuilder) Form

func (s *SqlBuilder) Form(tableName string) *SqlBuilder

func (*SqlBuilder) LeftJoin

func (s *SqlBuilder) LeftJoin(v string) *SqlBuilder

func (*SqlBuilder) Limit

func (s *SqlBuilder) Limit(pageNum, pageSize int) *SqlBuilder

func (*SqlBuilder) LimitNotPage

func (s *SqlBuilder) LimitNotPage(pageNum, pageSize int) *SqlBuilder

func (*SqlBuilder) Order

func (s *SqlBuilder) Order(v string) *SqlBuilder

Order 正序

func (*SqlBuilder) OrderBy

func (s *SqlBuilder) OrderBy(v string) *SqlBuilder

OrderBy 自由排序

func (*SqlBuilder) OrderDesc

func (s *SqlBuilder) OrderDesc(v string) *SqlBuilder

OrderDesc 倒序

func (*SqlBuilder) ToSql

func (s *SqlBuilder) ToSql() (string, map[string]interface{})

ToSql 转换为sql

func (*SqlBuilder) Where

func (s *SqlBuilder) Where(w, k string, v interface{}) *SqlBuilder

Where 首次拼接

type T added in v0.7.6

type T any

Jump to

Keyboard shortcuts

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