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
- func GetIDMachineID(id uint64) uint16
- func GetIDSequence(id uint64) uint8
- func GetIDTimestamp(id uint64) time.Time
- func GetInstanceID(uid uint64, prefix string) string
- func GetIntID() uint64
- func GetUUID36(prefix string) string
- func IsValidIDRange(id uint64, start, end time.Time) bool
- func NewRequestID() string
- func NewSecretID() string
- func NewSecretKey() string
- func NewSpanID() string
- func NewTraceID() string
- func ValidateIntID(id uint64) bool
- func ValidateIntIDStrict(id uint64) bool
- type ID
- func (id ID) Equal(other ID) bool
- func (ID) GormDBDataType(db string) string
- func (id ID) IsZero() bool
- func (id ID) MarshalJSON() ([]byte, error)
- func (id *ID) Scan(src interface{}) error
- func (id ID) String() string
- func (id ID) Uint64() uint64
- func (id *ID) UnmarshalJSON(b []byte) error
- func (id ID) Value() (driver.Value, error)
Constants ¶
View Source
const ( Alphabet62 = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890" Alphabet36 = "abcdefghijklmnopqrstuvwxyz1234567890" )
62进制字母表
Variables ¶
This section is empty.
Functions ¶
func GetIDMachineID ¶ added in v0.2.8
GetIDMachineID 从 ID 中提取机器 ID
func GetIDTimestamp ¶ added in v0.2.8
GetIDTimestamp 从 ID 中提取时间戳 返回 ID 生成的大致时间
func IsValidIDRange ¶ added in v0.2.8
IsValidIDRange 验证 ID 是否在指定的时间范围内
func NewRequestID ¶ added in v0.2.7
func NewRequestID() string
NewRequestID 生成请求 ID 格式:req-{timestamp}-{random}
func NewTraceID ¶ added in v0.2.7
func NewTraceID() string
NewTraceID 生成追踪 ID(32位十六进制字符串) 格式:trace-{timestamp}-{random}
func ValidateIntID ¶ added in v0.2.8
ValidateIntID 验证 uint64 类型的 ID 是否合法 基于 Sonyflake 的特性进行验证
func ValidateIntIDStrict ¶ added in v0.2.8
ValidateIntIDStrict 严格验证 uint64 类型的 ID 只接受由当前系统生成的 ID(时间范围更严格)
Types ¶
type ID ¶
type ID struct {
// contains filtered or unexported fields
}
func (ID) GormDBDataType ¶
GormDBDataType 实现 schema.GormDBDataTypeInterface,告诉 GORM 数据类型
func (ID) MarshalJSON ¶
MarshalJSON 实现 json.Marshaler 接口,输出数字
func (*ID) UnmarshalJSON ¶
UnmarshalJSON 实现 json.Unmarshaler 接口,接受数字
Click to show internal directories.
Click to hide internal directories.