osutil

package
v1.4.5 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2024 License: MIT Imports: 24 Imported by: 0

Documentation

Index

Constants

View Source
const (
	KiB // 1 KiB = 1024 Bytes
	MiB // 1 MiB = 1048576 Bytes
	GiB // 1 GiB = 1073741824 Bytes
	TiB // 1 TiB = 1099511627776 Bytes
	PiB // 1 PiB = 1125899906842624 Bytes
	EiB // 1 EiB = 1152921504606846976 Bytes
)

binary units (IEC 60027)

View Source
const (
	KB = 1000
	MB = 1_000_000
	GB = 1_000_000_000
	TB = 1_000_000_000_000
	PB = 1_000_000_000_000_000
	EB = 1_000_000_000_000_000_000
)
View Source
const EnvVarHome = "HOME"
View Source
const EnvVarTMP = "TMP"

Variables

View Source
var ErrorForceKill = errors.New("force killed failed")
View Source
var ID id

Functions

func AfterStopFunc

func AfterStopFunc(d time.Duration, f func(<-chan struct{}))

AfterStopFunc 超时停止协程,关闭协程。使用原始channel实现 这里新建了两个channel是防止关闭一个已经关闭的channel导致panic,这里还有优化点。 该函数主使用场景,比如防止扫描磁盘时间过长,在规定时间里获取结果.

func AfterStopWithContext

func AfterStopWithContext(d time.Duration, f func(context.Context))

AfterStopWithContext 超时停止协程,关闭协程。使用context实现.

func FileCopy

func FileCopy(source, dest string) (err error)

FileCopy 复制文件,代码仅供参考. Deprecated

func FileExist

func FileExist(file string) bool

FileExist 判断文件是否存在.

func FileFindPath

func FileFindPath(fname string) (string, error)

FileFindPath 获取文件名路径,首先判断文件是否可以直接访问,优先获取当前可执行文件夹下,再去找工作路径下.

func FileIsBinary

func FileIsBinary(content string) bool

FileIsBinary 判断文件是否是二进制文件.

func FileIsDir

func FileIsDir(path string) bool

FileIsDir 判断文件是否是目录.

func FileMove

func FileMove(src, dest string) error

FileMove 移动文件,代码仅供参考. Deprecated

func FileSize

func FileSize(path string) int64

FileSize 获取文件大小.

func FormatBinary

func FormatBinary(value int64) string

FormatBinary formats bytes integer to human readable string according to IEC 60027. For example, 31323 bytes will return 30.59KB.

func FormatBinaryDecimal

func FormatBinaryDecimal(value int64) string

FormatBinaryDecimal formats bytes integer to human readable string according to SI international system of units. For example, 31323 bytes will return 31.32KB.

func FormatByStr

func FormatByStr(tpl string, t int64) string

FormatByStr 将字符串中的时间变量(y年/m月/d日/h时/i分/s秒)转换成时间字符串.

func FormatBytesString

func FormatBytesString(b int64) string

FormatBytesString 格式化bytes单位成可阅读单位形式,由于电脑制造商使用的是1000为单位计算磁盘大小 所以基本上使用该函数格式化存储大小.

func FormatBytesStringOhMyGod

func FormatBytesStringOhMyGod(b int64) string

FormatBytesStringOhMyGod 格式化存储大小,理论上应该使用该方式格式化存储大小,但是实际上不是这样的,呜呜呜呜呜呜.

func GetMonthRange

func GetMonthRange(year, month int) (beginTime, endTime int64, err error)

GetMonthRange 获得指定年份和月份的起始unix时间和截止unix时间.

func GetWeek

func GetWeek(t time.Time) int

GetWeek 获得星期的数字.

func IsWindows

func IsWindows() bool

func MacAddr

func MacAddr() (upMac []string, err error)

MacAddr 获取机器mac地址,返回mac字串数组.

func NumberID

func NumberID() string

func ParseBinaryString

func ParseBinaryString(value string) (i int64, err error)

ParseBinaryString parses human readable bytes string to bytes integer. For example, 6GiB (6Gi is also valid) will return 6442450944.

func ParseBytes

func ParseBytes(value string) (int64, error)

ParseBytes parses human readable bytes string to bytes integer. For example, 6GiB (6Gi is also valid) will return 6442450944, and 6GB (6G is also valid) will return 6000000000.

func ParseStringDecimal

func ParseStringDecimal(value string) (i int64, err error)

ParseStringDecimal parses human readable bytes string to bytes integer. For example, 6GB (6G is also valid) will return 6000000000.

func RandAlphaString

func RandAlphaString(length int) string

RandAlphaString 生成随机长度字母. Deprecated

func RandBytes

func RandBytes(length int) []byte

RandBytes 生成随机长度字节.

func RandInt

func RandInt(min, max int) int

RandInt 指定范围内的随机数字.

func RandInt32

func RandInt32(min, max int32) int32

RandInt32 指定范围内的随机数字,max必须大于min.

func RandInt64

func RandInt64(min, max int64) int64

RandInt64 指定范围内的随机数字,max必须大于min.

func RandInts

func RandInts(from, to, size int) []int

RandInts 生成指定范围int类型数组.

func RandLower

func RandLower(l int) string

RandLower 指定长度的随机小写字母.

func RandString

func RandString(n int) string

RandString 生成随机长度字符串,推荐使用.

func RandUpper

func RandUpper(l int) string

RandUpper 指定长度的随机大写字母.

func Retry

func Retry(n uint, interval time.Duration, fn func() error) (err error)

Retry calls the `fn` and if it returns the error, retry to call `fn` after `interval` duration. The `fn` is called up to `n` times.

func Round

func Round(x float64, place int32) float64

Round 四舍五入.

func SlowFunc

func SlowFunc(threshold time.Duration, sf func() error, cb func()) error

SlowFunc 执行slowFunc函数,如果超过阈值会执行callback函数,不会等待sf函数

func TimeToStr

func TimeToStr(t time.Time, format ...string) string

TimeToStr 返回时间的字符串格式.

func TimestampToStr

func TimestampToStr(t int64, format ...string) string

TimestampToStr Timestamp将unix时间转为时间字符串.

func UUID

func UUID() string

func WithContext

func WithContext(ctx context.Context, n uint, interval time.Duration, fn func() error) (err error)

WithContext stops retrying when the context is done.

func WithContextFunc

func WithContextFunc(ctx context.Context, f func()) context.Context

WithContextFunc returns a copy of parent context that is cancelled when an os interrupt signal is received. The callback function f is invoked before cancellation.

func XID

func XID() string

Types

type CmdOutput

type CmdOutput struct {
	Stdout *bytes.Buffer
	Stderr *bytes.Buffer
}

func Exec

func Exec(opt *Options) (*CmdOutput, error)

type IDGenerator

type IDGenerator interface {
	Snowflake() snowflake.ID
	XID() xid.ID
	UUID4() uuid.UUID
	SInt64() int64
	SString() string
	XString() string
	UString() string
	RandString(int) string
}

type Options

type Options struct {
	CancelCtx  context.Context
	Command    string
	CliArgs    []string
	BinPath    string
	Env        map[string]string
	ErrWriter  io.Writer
	Stdin      io.Reader
	WorkingDir string
}

Jump to

Keyboard shortcuts

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