toolkit

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Sep 7, 2022 License: MIT Imports: 18 Imported by: 0

README

toolkit

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

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultPermOpen = os.FileMode(0666)
View Source
var LineBreak = []byte{'\n'}

Functions

func AddQuery

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

AddQuery url增加query参数

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 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 Md5File

func Md5File(path string) (string, error)

Md5File 计算文件的MD5值

func Mkdirs

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

Mkdirs 根据路径创建目录

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 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 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的数据写入文件

Types

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 Printer

type Printer func(time.Duration)

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