toolkit

package module
v1.0.0-pre1 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2022 License: MIT Imports: 30 Imported by: 0

README

toolkit

开发项目用到的工具库, 借鉴了部分开源项目的utility, 避免每次都造轮子

Documentation

Index

Constants

View Source
const (
	// IVDefaultValue is the default value for IV.
	IVDefaultValue = "I Love Go Frame!"
)

Variables

View Source
var DefaultPermOpen = os.FileMode(0666)
View Source
var ErrorCipherTextInvalid = fmt.Errorf("cipherText is not a multiple of the block size")
View Source
var ErrorCipherTextTooShort = fmt.Errorf("cipherText too short")
View Source
var ErrorInvalidBlockLen = fmt.Errorf("invalid blocklen")
View Source
var ErrorInvalidDataLen = fmt.Errorf("invalid data len")
View Source
var ErrorInvalidPadding = fmt.Errorf("invalid padding")
View Source
var LineBreak = []byte{'\n'}

Functions

func AddQuery

func AddQuery(rawUrl string, key string, value string) (string, error)

AddQuery url增加query参数

func BinarySearch

func BinarySearch[T types.Ordered](data []T, value T) int

BinarySearch 二分搜索

func CRC

func CRC(content []byte) uint32

CRC 计算bytes的CRC值

func Copy

func Copy(src interface{}) interface{}

Copy creates a deep copy of whatever is passed to it and returns the copy in an interface{}. The returned value will need to be asserted to the correct type.

func CopySlice

func CopySlice[T any](data []T) []T

CopySlice 复制切片

func Decrypt

func Decrypt(cipherText []byte, key []byte, iv ...[]byte) ([]byte, error)

Decrypt is alias of DecryptCBC.

func DecryptCBC

func DecryptCBC(cipherText []byte, key []byte, iv ...[]byte) ([]byte, error)

DecryptCBC decrypts `cipherText` using CBC mode. Note that the key must be 16/24/32 bit length. The parameter `iv` initialization vector is unnecessary.

func DecryptCFB

func DecryptCFB(cipherText []byte, key []byte, unPadding int, iv ...[]byte) ([]byte, error)

DecryptCFB decrypts `plainText` using CFB mode. Note that the key must be 16/24/32 bit length. The parameter `iv` initialization vector is unnecessary.

func Encrypt

func Encrypt(plainText []byte, key []byte, iv ...[]byte) ([]byte, error)

Encrypt is alias of EncryptCBC.

func EncryptCBC

func EncryptCBC(plainText []byte, key []byte, iv ...[]byte) ([]byte, error)

EncryptCBC encrypts `plainText` using CBC mode. Note that the key must be 16/24/32 bit length. The parameter `iv` initialization vector is unnecessary.

func EncryptCFB

func EncryptCFB(plainText []byte, key []byte, padding *int, iv ...[]byte) ([]byte, error)

EncryptCFB encrypts `plainText` using CFB mode. Note that the key must be 16/24/32 bit length. The parameter `iv` initialization vector is unnecessary.

func ExistDir

func ExistDir(name string) (bool, error)

ExistDir 判断目录是否存在

func ExistFile

func ExistFile(name string) (bool, error)

ExistFile 判断文件是否存在

func FanOut

func FanOut[T any](data types.IFanOut[T], number int) []T

func FileSize

func FileSize(file *os.File) (int64, error)

FileSize 通过打开的文件计算大小(Byte单位)

func FileSizeByPath

func FileSizeByPath(path string) (int64, error)

FileSizeByPath 通过路径计算文件的大小(Byte单位)

func FuncCost

func FuncCost(start time.Time, printer Printer)

FuncCost 获取函数的运行时间

func GetDateAgo

func GetDateAgo(ago int64, format ...string) string

GetDateAgo 获取n天前的时间

func GetTimeFromTimestamp

func GetTimeFromTimestamp(tm int64, format ...string) string

GetTimeFromTimestamp 根据时间戳打印对应的日期

func HashBytes

func HashBytes(content []byte, hashType ...string) string

HashBytes 计算bytes的CRC值

func HashFile

func HashFile(path string, hashType ...string) (string, error)

HashFile 计算文件的MD5值

func LogModule

func LogModule(module string, fields ...map[string]interface{}) *logrus.Entry

func LogWithCtx

func LogWithCtx(ctx context.Context, module string, fields ...map[string]interface{}) *logrus.Entry

func Max

func Max[T types.Ordered](data ...T) T

Max 求最大值

func Min

func Min[T types.Ordered](data ...T) T

Min 求最小值

func Mkdirs

func Mkdirs(path string, perms ...os.FileMode) error

Mkdirs 根据路径创建目录

func PKCS5Padding

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

func PKCS5UnPadding

func PKCS5UnPadding(src []byte, blockSize int) ([]byte, error)

func QueryUrl

func QueryUrl(rawUrl string, key string) (string, error)

QueryUrl 查询url的query参数

func ReadDir

func ReadDir(path string) ([]string, []string, error)

ReadDir 读取目录的内容,分别返回目录,文件的全路径集合

func ReadFile

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

ReadFile 读取文件的内容

func ReadLine

func ReadLine(buffer *bufio.Reader) ([]byte, error)

ReadLine 按行读取文件内容 为了提升性能,使用前需将文件转换为buffer reader file, err := os.Open(path)

if err != nil {
     return err
}

defer file.Close() buffer := bufio.NewReader(file) ReadLine(buffer)

func ReadLines

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

ReadLines 读取文件内容,按行返回

func ResponseFail

func ResponseFail[T any](c *gin.Context, httpCode int, code int, msg string, data ...T)

func ResponseFailDefault

func ResponseFailDefault[T any](c *gin.Context, msg string, data ...T)

func ResponseSuccess

func ResponseSuccess[T any](c *gin.Context, code int, msg string, data T)

func ResponseSuccessDefault

func ResponseSuccessDefault[T any](c *gin.Context, data T)

func ReverseSlice

func ReverseSlice[T any](data []T)

ReverseSlice 转置切片

func RunPeriod

func RunPeriod(interval time.Duration, handler func(), closeCh ...chan struct{})

RunPeriod 周期运行函数

func RunSafe

func RunSafe(handler func(), errHandler ...types.ErrHandler)

func ScanStd

func ScanStd(handler LineHandler, hints ...string) error

ScanStd 从标准输入读取内容

func SelectAll

func SelectAll[T any](data T, prefix ...string) string

func SliceEqual

func SliceEqual[T comparable](a []T, b []T) bool

SliceEqual 判断两个Slice是否完全一样

func SliceMax

func SliceMax[T types.Ordered](data []T) T

SliceMax 求数组的最大值

func SliceMin

func SliceMin[T types.Ordered](data []T) T

SliceMin 求数组的最小值

func SortComparator

func SortComparator[T types.IComparator](data T)

SortComparator 对实现了比较接口的类型排序

func SortSlice

func SortSlice[T types.Ordered](data []T, reverseOpts ...bool)

SortSlice 对切片排序, 切片必须是可以比较的类型

func SortSliceWithComparator

func SortSliceWithComparator[T any](data []T, comparator func(i, j int) bool)

SortSliceWithComparator 针对切片的自定义排序

func SplitFile

func SplitFile(path string) (string, string, error)

SplitFile 获取文件的目录和文件名

func UniqueSlice

func UniqueSlice[T comparable](data []T) []T

UniqueSlice 去重切片

func WaitGroup

func WaitGroup(number int, handler types.WaitGroupHandler, closeHandler ...func()) error

func WriteFile

func WriteFile(path string, content []byte, append bool) (int64, error)

WriteFile 将content的数据写入文件

func ZeroPadding

func ZeroPadding(cipherText []byte, blockSize int) ([]byte, int)

func ZeroUnPadding

func ZeroUnPadding(plaintext []byte, unPadding int) []byte

Types

type BuiltinSet

type BuiltinSet[K comparable] map[K]struct{}

BuiltinSet is an associative container that contains an unordered set of unique objects of type K.

func SetOf

func SetOf[K comparable](ks ...K) BuiltinSet[K]

SetOf creates a new BuiltinSet object with the initial content from ks.

func (BuiltinSet[K]) Clear

func (s BuiltinSet[K]) Clear()

Clear implements the Container interface.

func (BuiltinSet[K]) Delete

func (s BuiltinSet[K]) Delete(k K)

Delete deletes an element from the set. It returns nothing, so it's faster than Remove.

func (BuiltinSet[K]) Difference

func (s BuiltinSet[K]) Difference(other BuiltinSet[K]) BuiltinSet[K]

Difference returns a new set with elements in the set that are not in other.

func (BuiltinSet[K]) ForEach

func (s BuiltinSet[K]) ForEach(cb func(k K))

ForEach implements the Set interface.

func (BuiltinSet[K]) ForEachIf

func (s BuiltinSet[K]) ForEachIf(cb func(k K) bool)

ForEachIf implements the Container interface.

func (BuiltinSet[K]) Has

func (s BuiltinSet[K]) Has(k K) bool

Has implements the Set interface.

func (BuiltinSet[K]) Insert

func (s BuiltinSet[K]) Insert(k K) bool

Insert implements the Set interface.

func (BuiltinSet[K]) InsertN

func (s BuiltinSet[K]) InsertN(ks ...K) int

InsertN implements the Set interface.

func (BuiltinSet[K]) Intersection

func (s BuiltinSet[K]) Intersection(other BuiltinSet[K]) BuiltinSet[K]

Intersection returns a new set with elements common to the set and other.

func (BuiltinSet[K]) IsDisjointOf

func (s BuiltinSet[K]) IsDisjointOf(other BuiltinSet[K]) bool

IsDisjointOf return True if the set has no elements in common with other. Sets are disjoint if and only if their intersection is the empty set.

func (BuiltinSet[K]) IsEmpty

func (s BuiltinSet[K]) IsEmpty() bool

IsEmpty implements the Container interface.

func (BuiltinSet[K]) IsSubsetOf

func (s BuiltinSet[K]) IsSubsetOf(other BuiltinSet[K]) bool

IsSubsetOf tests whether every element in the set is in other.

func (BuiltinSet[K]) IsSupersetOf

func (s BuiltinSet[K]) IsSupersetOf(other BuiltinSet[K]) bool

IsSupersetOf tests whether every element in other is in the set.

func (BuiltinSet[K]) Keys

func (s BuiltinSet[K]) Keys() []K

Keys return a copy of all keys as a slice.

func (BuiltinSet[K]) Len

func (s BuiltinSet[K]) Len() int

Len implements the Container interface.

func (BuiltinSet[K]) Remove

func (s BuiltinSet[K]) Remove(k K) bool

Remove implements the Set interface.

func (BuiltinSet[K]) RemoveN

func (s BuiltinSet[K]) RemoveN(ks ...K) int

RemoveN implements the Set interface.

func (BuiltinSet[K]) String

func (s BuiltinSet[K]) String() string

String implements the fmt.Stringer interface.

func (BuiltinSet[K]) Union

func (s BuiltinSet[K]) Union(other BuiltinSet[K]) BuiltinSet[K]

Union returns a new set with elements from the set and other.

func (BuiltinSet[K]) Update

func (s BuiltinSet[K]) Update(other BuiltinSet[K])

Update adds all elements from other to set. set |= other.

type CounterMap

type CounterMap[K comparable] map[K]int

func NewCounterMap

func NewCounterMap[K comparable](data []K) CounterMap[K]

func (CounterMap[K]) Equal

func (c CounterMap[K]) Equal(other CounterMap[K]) bool

type EUnit

type EUnit[K any] struct {
	Raw   K
	Field []byte
}

func SortFieldsWithGbk

func SortFieldsWithGbk[K any](units []EUnit[K], offset int, limit int, desc bool) ([]EUnit[K], error)

SortFieldsWithGbk 按照中文排序

type IBufferPool

type IBufferPool interface {
	Get(n int) []byte
	Put(b []byte)
}

func NewBufferPool

func NewBufferPool(baseline int) IBufferPool

type ICacheBuffer

type ICacheBuffer[T any] interface {
	Close()
	Get() (T, error)
}

func NewCacheBuffer

func NewCacheBuffer[T any](fetcher types.FetchHandler[T]) ICacheBuffer[T]

type ILimit

type ILimit interface {
	Put()
	Get()
}

func NewLimit

func NewLimit(max int) ILimit

type IPriorityChan

type IPriorityChan[T any] interface {
	Get() T
	TryGet() T
	GetWithTimeout(timeout time.Duration) (T, error)
	HandleSignal(highHandler types.PriorityHandler, lowHandler types.PriorityHandler) error
}

func NewPriorityChan

func NewPriorityChan[T any](size int) IPriorityChan[T]

type Interface

type Interface interface {
	DeepCopy() interface{}
}

Interface for delegating copy process to type

type LineHandler

type LineHandler func(string) error

type ListNode

type ListNode[T any] struct {
	Data *T
	Next *ListNode[T]
	Pre  *ListNode[T]
}

func (*ListNode[T]) Pop

func (list *ListNode[T]) Pop() *ListNode[T]

func (*ListNode[T]) PushBack

func (list *ListNode[T]) PushBack(data *ListNode[T])

func (*ListNode[T]) PushFront

func (list *ListNode[T]) PushFront(data *ListNode[T])

type Printer

type Printer func(time.Duration)

type SecTimestamp

type SecTimestamp int64

SecTimestamp 用于mysql的秒时间戳类型

func NewSecTimestamp

func NewSecTimestamp(t time.Time) SecTimestamp

func (*SecTimestamp) Scan

func (s *SecTimestamp) Scan(src interface{}) error

func (*SecTimestamp) Time

func (s *SecTimestamp) Time() time.Time

func (*SecTimestamp) Value

func (s *SecTimestamp) Value() (driver.Value, error)

type SortedMap

type SortedMap[K types.Ordered, V any] struct {
	ReverseOpt bool
	Tuples     []Tuple[K, V]
	RawMap     map[K]V
}

SortedMap 有序Map,底层维护了有序切片 如果需要对map进行修改需要执行Rebuild来维护有序行,否则会导致不一致

func NewSortedMap

func NewSortedMap[K types.Ordered, V any](data map[K]V, reverseOpts ...bool) SortedMap[K, V]

func (*SortedMap[K, V]) Rebuild

func (s *SortedMap[K, V]) Rebuild()

Rebuild 重新构建有序Map,一般用于map修改后再次维护tuples的有序行

type Tuple

type Tuple[K types.Ordered, V any] struct {
	Key   K
	Value V
}

Tuple 有序元组,用于map的平铺,方便排序

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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