idutil

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Dec 11, 2025 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package idutil 提供各种 ID 生成和验证工具

ID 验证

## uint64 类型 ID 验证

基本验证(推荐用于外部输入):

valid := idutil.ValidateIntID(id)
if !valid {
	return errors.New("invalid ID")
}

验证规则:

  • ID 不能为 0
  • ID 不能超过最大值(2^63-1)
  • 时间戳不能早于 2014-09-01(Sonyflake 起始时间)
  • 时间戳不能晚于当前时间 1 小时(考虑时钟偏差)

严格验证(用于系统内部生成的 ID):

valid := idutil.ValidateIntIDStrict(id)

严格规则:

  • 包含基本验证的所有规则
  • ID 时间必须在过去 30 天到未来 1 分钟之间

时间范围验证:

start := time.Now().Add(-7 * 24 * time.Hour)
end := time.Now()
valid := idutil.IsValidIDRange(id, start, end)

ID 解析

从 uint64 ID 中提取信息:

// 提取生成时间
timestamp := idutil.GetIDTimestamp(id)

// 提取机器 ID
machineID := idutil.GetIDMachineID(id)

// 提取序列号
sequence := idutil.GetIDSequence(id)

使用示例

验证用户提交的 ID:

func GetUser(idStr string) (*User, error) {
	id, err := strconv.ParseUint(idStr, 10, 64)
	if err != nil {
		return nil, errors.New("invalid ID format")
	}

	if !idutil.ValidateIntID(id) {
		return nil, errors.New("invalid ID")
	}

	// 查询数据库...
}

Index

Constants

View Source
const (
	Alphabet62 = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
	Alphabet36 = "abcdefghijklmnopqrstuvwxyz1234567890"
)

62进制字母表

Variables

This section is empty.

Functions

func GetIDMachineID added in v0.2.8

func GetIDMachineID(id uint64) uint16

GetIDMachineID 从 ID 中提取机器 ID

func GetIDSequence added in v0.2.8

func GetIDSequence(id uint64) uint8

GetIDSequence 从 ID 中提取序列号

func GetIDTimestamp added in v0.2.8

func GetIDTimestamp(id uint64) time.Time

GetIDTimestamp 从 ID 中提取时间戳 返回 ID 生成的大致时间

func GetInstanceID

func GetInstanceID(uid uint64, prefix string) string

GetInstanceID 获取实例ID

func GetIntID

func GetIntID() uint64

GetIntID 获取雪花算法生成的唯一ID 在测试环境或雪花算法不可用时,返回基于时间戳的ID

func GetUUID36

func GetUUID36(prefix string) string

GetUUID36 获取36进制ID

func IsValidIDRange added in v0.2.8

func IsValidIDRange(id uint64, start, end time.Time) bool

IsValidIDRange 验证 ID 是否在指定的时间范围内

func NewRequestID added in v0.2.7

func NewRequestID() string

NewRequestID 生成请求 ID 格式:req-{timestamp}-{random}

func NewSecretID

func NewSecretID() string

生成36位随机字符串

func NewSecretKey

func NewSecretKey() string

生成32位随机字符串

func NewSpanID added in v0.2.7

func NewSpanID() string

NewSpanID 生成 Span ID(16位十六进制字符串)

func NewTraceID added in v0.2.7

func NewTraceID() string

NewTraceID 生成追踪 ID(32位十六进制字符串) 格式:trace-{timestamp}-{random}

func ValidateIntID added in v0.2.8

func ValidateIntID(id uint64) bool

ValidateIntID 验证 uint64 类型的 ID 是否合法 基于 Sonyflake 的特性进行验证

func ValidateIntIDStrict added in v0.2.8

func ValidateIntIDStrict(id uint64) bool

ValidateIntIDStrict 严格验证 uint64 类型的 ID 只接受由当前系统生成的 ID(时间范围更严格)

Types

type ID

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

func NewID

func NewID(value uint64) ID

NewID 创建一个新的 ID 实例

func (ID) Equal

func (id ID) Equal(other ID) bool

Equal 比较两个 ID 是否相等

func (ID) GormDBDataType

func (ID) GormDBDataType(db string) string

GormDBDataType 实现 schema.GormDBDataTypeInterface,告诉 GORM 数据类型

func (ID) IsZero

func (id ID) IsZero() bool

IsZero 是否为零值

func (ID) MarshalJSON

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

MarshalJSON 实现 json.Marshaler 接口,输出数字

func (*ID) Scan

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

Scan 实现 sql.Scanner 接口,从数据库整数读取

func (ID) String

func (id ID) String() string

String 返回 ID 的字符串表示

func (ID) Uint64

func (id ID) Uint64() uint64

Uint64 返回 ID 的 uint64 值

func (*ID) UnmarshalJSON

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

UnmarshalJSON 实现 json.Unmarshaler 接口,接受数字

func (ID) Value

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

Value 实现 driver.Valuer 接口,将 ID 写入数据库

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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