sid

package
v2.0.62 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Epoch           int64 = 1288834974657
	DefaultNodeBits uint8 = 10
	DefaultStepBits uint8 = 12
	EncodeAlphabet        = "ABCDEFGHJKLMNPQRSTUVWXYZ123456789"
)

常量定义

Variables

View Source
var (
	// 预计算错误
	ErrInvalidNode      = errors.New("invalid node ID")
	ErrBitsAllocation   = errors.New("nodeBits + stepBits cannot exceed 22")
	ErrClockBackwards   = errors.New("clock moved backwards")
	ErrInvalidCharacter = errors.New("invalid character for encoding")
)

Functions

This section is empty.

Types

type Config

type Config struct {
	NodeBits uint8
	StepBits uint8
}

Config 配置选项

type ID

type ID int64

ID 雪花ID类型

func Decode

func Decode(s string) (ID, error)

Decode 从编码字符串解析ID

func ParseString

func ParseString(s string) (ID, error)

ParseString 从字符串解析ID

func (ID) Build

func (id ID) Build(builder clause.Builder)

func (ID) Encode

func (id ID) Encode() string

Encode 使用自定义base32编码ID为短字符串

func (ID) Expression

func (id ID) Expression() clause.Expression

Expression 实现 GORM 的 Expression 接口,让 ID 在查询中自动转换为 int64

func (ID) GormDBDataType

func (ID) GormDBDataType(db *gorm.DB, field *schema.Field) string

GormDBDataType gorm 方言映射

func (ID) GormValue

func (id ID) GormValue(ctx context.Context, db *gorm.DB) clause.Expr

GormValue 添加 GORM 特定的方法

func (ID) Int64

func (id ID) Int64() int64

Int64 返回int64形式的ID

func (*ID) IsNil

func (id *ID) IsNil() bool

func (*ID) IsValid

func (id *ID) IsValid() bool

func (*ID) IsZero

func (id *ID) IsZero() bool

func (ID) MarshalJSON

func (id ID) MarshalJSON() ([]byte, error)

func (ID) NodeID

func (id ID) NodeID(nodeShift, nodeMask uint8) int64

NodeID 提取ID中的节点ID部分

func (*ID) Scan

func (id *ID) Scan(value interface{}) error

func (ID) Step

func (id ID) Step(stepMask int64) int64

Step 提取ID中的序列号部分

func (ID) String

func (id ID) String() string

String 返回十进制字符串表示

func (ID) Time

func (id ID) Time(epoch time.Time) time.Time

Time 提取ID中的时间戳部分

func (*ID) UnmarshalJSON

func (id *ID) UnmarshalJSON(data []byte) error

func (ID) Value

func (id ID) Value() (driver.Value, error)

实现 GORM 的序列化接口

type JSONSyntaxError

type JSONSyntaxError struct {
	Original []byte
}

JSON序列化相关

func (JSONSyntaxError) Error

func (e JSONSyntaxError) Error() string

type SnowflakeID

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

SnowflakeID 雪花ID生成器

func New

func New(nodeID int64, config ...Config) (*SnowflakeID, error)

NewSnowflakeID 创建新的雪花ID生成器

func (*SnowflakeID) Generate

func (s *SnowflakeID) Generate() (ID, error)

Generate 生成新的雪花ID

func (*SnowflakeID) MustGenerate

func (s *SnowflakeID) MustGenerate() ID

MustGenerate 生成ID,如果出错会panic(适用于确定不会出错的场景)

Jump to

Keyboard shortcuts

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