common

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 31, 2022 License: GPL-3.0 Imports: 27 Imported by: 0

Documentation

Index

Constants

View Source
const NBUF = 512

Variables

View Source
var ErrLineRange = errors.New("go-util.file: line range error")

Functions

func AddHeadContent added in v1.0.1

func AddHeadContent(content []byte, head []byte) []byte

文件头部添加内容

func AddTailContent added in v1.0.1

func AddTailContent(content []byte, tail []byte) []byte

文件尾部添加内容

func Any2String added in v1.0.5

func Any2String(a any) string

func CatFile

func CatFile(fileName string, w io.Writer)

CatFile data from file write to the Writer

func CompressAndUncompress

func CompressAndUncompress(src []byte)

func CopyFile

func CopyFile(dstName, srcName string) (written int64, err error)

func DecodeHexOrBase64

func DecodeHexOrBase64(content string) ([]byte, error)

func DeleteLines added in v1.0.1

func DeleteLines(content []byte, lr LineRange) ([]byte, error)

func DurationToTime

func DurationToTime(ms int64, duration time.Duration) time.Time

func Exists

func Exists(path string) bool

判断所给路径文件/文件夹是否存在

func FindDigits

func FindDigits(filename string) []byte

FindDigits find digits from file 切片的底层指向一个数组,该数组的实际体积可能要大于切片所定义的体积。 只有在没有任何切片指向的时候,底层的数组内层才会被释放,这种特性有时会导致程序占用多余的内存。

func GracefulFprintf

func GracefulFprintf(data ...interface{}) string

func ISSupportDuration

func ISSupportDuration(duration time.Duration) bool

func IsDir

func IsDir(path string) bool

判断所给路径是否为文件夹

func IsFile

func IsFile(path string) bool

判断所给路径是否为文件

func KeyValue

func KeyValue(buf *bytes.Buffer, val reflect.Value)

func LocalIp

func LocalIp() (string, error)

func MapToString

func MapToString(a map[interface{}]interface{}) (string, error)

func MillisecondToTime

func MillisecondToTime(ms int64) time.Time

func Mkdir

func Mkdir(path string) error

func NewContext

func NewContext() (context.Context, context.CancelFunc)

NewContext 创建一个Context

func NewContextWithTimeout

func NewContextWithTimeout(n time.Duration) (context.Context, context.CancelFunc)

func RandomBytes

func RandomBytes(n int, alphabets ...byte) []byte

RandomBytes generate random []byte by specify chars.

func ReadFile

func ReadFile(filename string) []string

func Sieve

func Sieve() chan int

输出的素数

func Sort added in v1.0.1

func Sort(ids []uint32)

func StartCPUProfile

func StartCPUProfile(fileName string) error

func StringReverse

func StringReverse(src string) string

StringReverse support uft-8 string

func SubString

func SubString(src string, begin int) string

func SubStringRange

func SubStringRange(src string, begin, end int) string

func TimeToDuration

func TimeToDuration(t time.Time, duration time.Duration) int64

func TimeToMillisecond

func TimeToMillisecond(t time.Time) int64

length is 13,time

func ToBytes

func ToBytes(key string) ([]byte, error)

ToBytes 有4个字节的指针

func ToInt8s

func ToInt8s(key string) (is []int8, err error)

ToInt8s 有4个字节的指针

func ToString

func ToString(a interface{}) (string, error)

func Walk added in v1.0.1

func Walk(root string, recursive bool, suffix string, walkFn WalkFunc) error

遍历访问指定文件夹下的文件 @param root 需要遍历访问的文件夹 @param recursive 是否递归访问子文件夹 @param suffix 指定文件名后缀进行过滤,如果为"",则不过滤

func ZlibCompress

func ZlibCompress(src []byte) []byte

func ZlibRead added in v1.0.1

func ZlibRead(in []byte) (ret []byte)

func ZlibUnCompress

func ZlibUnCompress(compressSrc []byte) []byte

func ZlibWrite added in v1.0.1

func ZlibWrite(in []byte) []byte

Types

type Compressor added in v1.0.1

type Compressor interface {
	// 将整型切片压缩成二进制字节切片
	Compress(ids []uint32) (ret []byte)
	// 将二进制字节切片反序列化为整型切片
	// 反序列化后得到的整型切片,切片中整型的顺序和序列化之前保持不变
	UnCompress(b []byte) (ids []uint32)
}

具体使用见 LFCompressor 和 OriginCompressor

type LFCompressor added in v1.0.1

type LFCompressor struct {
	FB      uint32 // 用几个字节的 bit 表示跟随的数据
	ZlibExt bool   // 压缩之后,是否再用 zlib 进一步压缩
	// contains filtered or unexported fields
}

func (*LFCompressor) Compress added in v1.0.1

func (lfc *LFCompressor) Compress(ids []uint32) (ret []byte)

传入的整型切片必须是从小到大有序排列

func (*LFCompressor) UnCompress added in v1.0.1

func (lfc *LFCompressor) UnCompress(b []byte) (ids []uint32)

type LineRange added in v1.0.1

type LineRange struct {
	From int
	To   int
}

行号范围 1表示首行,-1表示最后一行

type OriginCompressor added in v1.0.1

type OriginCompressor struct {
	ZlibExt bool // 压缩之后,是否再用 zlib 进一步压缩
}

func (*OriginCompressor) Compress added in v1.0.1

func (oc *OriginCompressor) Compress(ids []uint32) (ret []byte)

并不强制要求整型切片有序

func (*OriginCompressor) UnCompress added in v1.0.1

func (oc *OriginCompressor) UnCompress(b []byte) (ids []uint32)

type Supervisor

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

func MakeSupervisor

func MakeSupervisor(maxRestartTime uint) Supervisor

MakeSupervisor create goroutine supervisor that proper panic

func (*Supervisor) Run

func (s *Supervisor) Run(f func(), info string)

type WalkFunc added in v1.0.1

type WalkFunc func(path string, info os.FileInfo, content []byte, err error) []byte

@param path 带路径的文件名 @param info 文件的 os.FileInfo 信息 @param content 文件内容 @return 返回nil或者content原始内容,则不修改文件内容,返回其他内容,则会覆盖重写文件

Jump to

Keyboard shortcuts

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