ulidgen

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jul 21, 2025 License: Apache-2.0 Imports: 4 Imported by: 0

README

ulidgen 组件

用于生成唯一、可排序、长度可控的 ULID 字符串,适用于订单号、流水号等业务场景。

特点

  • 基于 oklog/ulid 实现,唯一性强,26位标准长度。
  • 支持自定义长度(最短18位),可带业务前缀,兼容第三方API长度要求。
  • 可自定义随机源,便于测试。

安装依赖

go get github.com/oklog/ulid/v2

用法示例

import "yourmodule/ulidgen"

// 生成标准 ULID
id, err := ulidgen.GenerateULID()
// 生成18位短ULID
id, err := ulidgen.GenerateShortULID(18)
// 生成带前缀且总长20位的ULID
id, err := ulidgen.GenerateULIDWithPrefix("JYI", 20)

接口说明

  • GenerateULID() (string, error):生成26位标准ULID。
  • GenerateShortULID(length int) (string, error):生成指定长度(18~26位)ULID。
  • GenerateULIDWithPrefix(prefix string, maxLen int) (string, error):生成带前缀且总长不超过maxLen的ULID。
  • GenerateULIDWithRandSource(t time.Time, r *rand.Rand) string:自定义时间和随机源,便于测试。

测试

go test ./ulidgen

典型场景

  • 订单号、流水号、分布式唯一ID
  • 兼容外部API长度要求(如18~20位)

注意事项

  • 截断ULID会略微降低唯一性,但18位已足够大多数业务场景。
  • 如需更短ID,建议加业务前缀+时间戳+自增号等混合方案。

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GenerateShortULID

func GenerateShortULID(length int) (string, error)

GenerateShortULID 生成一个自定义长度的 ULID 字符串,最短 18 位,最长 26 位。 若 length < 18,则自动调整为 18;若 length > 26,则自动调整为 26。

func GenerateULID

func GenerateULID() (string, error)

GenerateULID 生成一个 ULID 字符串,长度为 26 位(标准 ULID 长度)。

func GenerateULIDWithPrefix

func GenerateULIDWithPrefix(prefix string, maxLen int) (string, error)

GenerateULIDWithPrefix 生成带前缀的 ULID,自动截断保证总长度不超过 maxLen。 若 prefix+ulid 超过 maxLen,则 ulid 部分会被截断。

func GenerateULIDWithRandSource

func GenerateULIDWithRandSource(t time.Time, r *math_rand.Rand) string

GenerateULIDWithRandSource 允许自定义随机源,便于测试。

Types

This section is empty.

Jump to

Keyboard shortcuts

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