utils

package
v0.0.0-...-d8b0a59 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2026 License: MIT Imports: 16 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// Epoch is set to the twitter snowflake epoch of Nov 04 2010 01:42:54 UTC in milliseconds
	// You may customize this to set a different epoch for your application.
	Epoch int64 = 1288834974657

	// NodeBits holds the number of bits to use for SnowNode
	// Remember, you have a total 22 bits to share between SnowNode/Step
	NodeBits uint8 = 10

	// StepBits holds the number of bits to use for Step
	// Remember, you have a total 22 bits to share between SnowNode/Step
	StepBits uint8 = 12
)
View Source
var ErrInvalidBase32 = errors.New("invalid base32")

ErrInvalidBase32 is returned by ParseBase32 when given an invalid []byte

View Source
var ErrInvalidBase58 = errors.New("invalid base58")

ErrInvalidBase58 is returned by ParseBase58 when given an invalid []byte

Functions

func Bytes2Data

func Bytes2Data[T any](bytes []byte) (T, error)

func CreateProductZapLogger

func CreateProductZapLogger(op ...Option) (*zap.Logger, zap.AtomicLevel, error)

CreateProductZapLogger 创建一个生产级别的 zap 日志记录器。 返回的 zap.AtomicLevel 可用于运行时动态修改日志等级。

func FileExists

func FileExists(filename string) bool

func NativelyLittle

func NativelyLittle() bool

NativelyLittle 返回当前系统的字节序是否为 little-endian

func NewZapWriter

func NewZapWriter(logger *zap.Logger, level zapcore.Level) io.Writer

NewZapWriter 创建一个新的 io.Writer,输出到 zap.Logger level: 日志级别 (zap.InfoLevel, zap.WarnLevel 等)

func PathExist

func PathExist(path string) bool

PathExist 判断路径是否存在

Types

type AtomicInt

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

func NewAtomicInt

func NewAtomicInt(val int) *AtomicInt

func NewAtomicInt64

func NewAtomicInt64(val int64) *AtomicInt

func (*AtomicInt) Add

func (a *AtomicInt) Add(delta int)

func (*AtomicInt) CompareAndSwap

func (a *AtomicInt) CompareAndSwap(oldVal, newVal int) bool

func (*AtomicInt) CompareAndSwap64

func (a *AtomicInt) CompareAndSwap64(oldVal, newVal int64) bool

func (*AtomicInt) Dec

func (a *AtomicInt) Dec() int

func (*AtomicInt) Get

func (a *AtomicInt) Get() int

func (*AtomicInt) Get64

func (a *AtomicInt) Get64() int64

func (*AtomicInt) Inc

func (a *AtomicInt) Inc() int

func (*AtomicInt) Load

func (a *AtomicInt) Load() int

func (*AtomicInt) Reset

func (a *AtomicInt) Reset()

func (*AtomicInt) Set

func (a *AtomicInt) Set(val int)

func (*AtomicInt) Set64

func (a *AtomicInt) Set64(val int64)

func (*AtomicInt) Store

func (a *AtomicInt) Store(val int)

func (*AtomicInt) String

func (a *AtomicInt) String() string

func (*AtomicInt) Swap

func (a *AtomicInt) Swap(newVal int) int

func (*AtomicInt) Swap64

func (a *AtomicInt) Swap64(newVal int64) int64

type JSONSyntaxError

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

A JSONSyntaxError is returned from UnmarshalJSON if an invalid ID is provided.

func (JSONSyntaxError) Error

func (j JSONSyntaxError) Error() string

type LogConfig

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

type Option

type Option interface {
	// contains filtered or unexported methods
}

An Option configures a Logger.

func SetConsoleWriterSyncer

func SetConsoleWriterSyncer(consoleWriter bool) Option

func SetLogCompress

func SetLogCompress(compress bool) Option

SetLogCompress 设置是否压缩日志文件

func SetLogFilename

func SetLogFilename(filename string) Option

SetLogFilename 设置日志文件名

func SetLogLevel

func SetLogLevel(level zapcore.Level) Option

SetLogLevel 设置日志级别

func SetLogLevelKey

func SetLogLevelKey(levelKey string) Option

SetLogLevelKey 设置日志级别字段名

func SetLogMaxAge

func SetLogMaxAge(maxAge int) Option

SetLogMaxAge 设置日志文件最大保存天数

func SetLogMaxBackups

func SetLogMaxBackups(maxBackups int) Option

SetLogMaxBackups 设置日志文件最大备份数

func SetLogMaxSize

func SetLogMaxSize(maxSize int) Option

SetLogMaxSize 设置日志文件最大大小(MB)

type SnowID

type SnowID int64

An SnowID is a custom type used for a snowflake SnowID. This is used so we can attach methods onto the SnowID.

func ParseBase2

func ParseBase2(id string) (SnowID, error)

ParseBase2 converts a Base2 string into a snowflake SnowID

func ParseBase32

func ParseBase32(b []byte) (SnowID, error)

ParseBase32 parses a base32 []byte into a snowflake SnowID NOTE: There are many different base32 implementations so becareful when doing any interoperation.

func ParseBase36

func ParseBase36(id string) (SnowID, error)

ParseBase36 converts a Base36 string into a snowflake SnowID

func ParseBase58

func ParseBase58(b []byte) (SnowID, error)

ParseBase58 parses a base58 []byte into a snowflake SnowID

func ParseBase64

func ParseBase64(id string) (SnowID, error)

func ParseBytes

func ParseBytes(id []byte) (SnowID, error)

func ParseInt64

func ParseInt64(id int64) SnowID

ParseInt64 converts an int64 into a snowflake SnowID

func ParseIntBytes

func ParseIntBytes(id [8]byte) SnowID

ParseIntBytes converts an array of bytes encoded as big endian integer as a snowflake SnowID

func ParseString

func ParseString(id string) (SnowID, error)

ParseString converts a string into a snowflake SnowID

func (SnowID) Base2

func (f SnowID) Base2() string

Base2 returns a string base2 of the snowflake SnowID

func (SnowID) Base32

func (f SnowID) Base32() string

Base32 uses the z-base-32 character set but encodes and decodes similar to base58, allowing it to create an even smaller result string. NOTE: There are many different base32 implementations so becareful when doing any interoperation.

func (SnowID) Base36

func (f SnowID) Base36() string

Base36 returns a base36 string of the snowflake SnowID

func (SnowID) Base58

func (f SnowID) Base58() string

Base58 returns a base58 string of the snowflake SnowID

func (SnowID) Base64

func (f SnowID) Base64() string

func (SnowID) Bytes

func (f SnowID) Bytes() []byte

func (SnowID) Int64

func (f SnowID) Int64() int64

Int64 returns an int64 of the snowflake SnowID

func (SnowID) IntBytes

func (f SnowID) IntBytes() [8]byte

IntBytes returns an array of bytes of the snowflake SnowID, encoded as a big endian integer.

func (SnowID) MarshalJSON

func (f SnowID) MarshalJSON() ([]byte, error)

MarshalJSON returns a json byte array string of the snowflake SnowID.

func (SnowID) Node

func (f SnowID) Node() int64

Node SnowNode returns an int64 of the snowflake SnowID node number DEPRECATED: the below function will be removed in a future release.

func (SnowID) Step

func (f SnowID) Step() int64

Step returns an int64 of the snowflake step (or sequence) number DEPRECATED: the below function will be removed in a future release.

func (SnowID) String

func (f SnowID) String() string

func (SnowID) Time

func (f SnowID) Time() int64

Time returns an int64 unix timestamp in milliseconds of the snowflake SnowID time DEPRECATED: the below function will be removed in a future release.

func (*SnowID) UnmarshalJSON

func (f *SnowID) UnmarshalJSON(b []byte) error

UnmarshalJSON converts a json byte array of a snowflake SnowID into an SnowID type.

type SnowNode

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

A SnowNode struct holds the basic information needed for a snowflake generator node

func NewSnowNode

func NewSnowNode(node int64) (*SnowNode, error)

NewSnowNode returns a new snowflake node that can be used to generate snowflake IDs

func (*SnowNode) Generate

func (n *SnowNode) Generate() SnowID

Generate creates and returns a unique snowflake SnowID To help guarantee uniqueness - Make sure your system is keeping accurate system time - Make sure you never have multiple nodes running with the same node SnowID

type ZapWriter

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

ZapWriter 将 io.Writer 接口桥接到 zap.Logger

func (*ZapWriter) Flush

func (w *ZapWriter) Flush()

Flush 将剩余缓冲区内容输出(例如程序退出时调用)

func (*ZapWriter) Write

func (w *ZapWriter) Write(p []byte) (n int, err error)

Write 实现 io.Writer 接口

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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