idKit

package
v3.1.110 Latest Latest
Warning

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

Go to latest
Published: Dec 23, 2024 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// Decompose returns a set of Sonyflake ID parts.
	Decompose func(id uint64) map[string]uint64 = sonyflake.Decompose
)

Functions

func NewCustomizedNanoId

func NewCustomizedNanoId(alphabet string, size int) (string, error)

NewCustomizedNanoId 生成定制化的NanoId

@param alphabet 字母表(默认为"_-0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") @param size 生成NanoId字符串的长度

func NewCustomizedULID

func NewCustomizedULID(ms uint64, entropy io.Reader) (string, error)

func NewNanoId

func NewNanoId(l ...int) (string, error)

NewNanoId 生成NanoId

NanoID 了解一下?比 UUID 更好用! - https://mp.weixin.qq.com/s/4muEuUkk3tq6iJXLwspQyQ

@param l 生成NanoId字符串的长度,不传参则默认长度为21(!!!:传参个数只能为0或1,且值>=0) @return 返回值的字母表为: "_-0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

e.g. () => ("4spQ-UdpjbhSjE046w1Ij", nil)

func NewSimpleUUID

func NewSimpleUUID() string

NewSimpleUUID UUIDv4,去掉了其中所有"-"

@return 长度32

e.g.

() => "415ef754dc174b888b186873e093ced1"

func NewSonyFlake

func NewSonyFlake(settings *sonyflake.Settings) (*sonyflake.Sonyflake, error)

NewSonyFlake 雪花算法.

PS: (1) 可用作分布式唯一id(前提是合理配置好 MachineID); (2) 通过 sonyflake.Sonyflake.NextID() 生成id(貌似是18位的).

golang实现的雪花算法 https://mp.weixin.qq.com/s/visG_GHtU67xCtsvvG1aPQ

@param settings 可以为nil(但不推荐这么干)

func NewULID

func NewULID() string

NewULID ULID(不建议用作分布式唯一id,小概率会重复)

PS: (1) 重复概率非常低,但不建议用作分布式唯一id(可以用作本地唯一id). (2) Format: tttttttttteeeeeeeeeeeeeeee where t is time and e is entropy.(时间+随机数) (3) If you just want to generate a ULID and don't (yet) care about details like performance, cryptographic security, etc., use the ulid.Make helper function.

This function calls time.Now to get a timestamp, and uses a source of entropy which is process-global, pseudo-random, and monotonic.

@return 长度: 26(即 ulid.EncodedSize)

e.g. () => "01GMSRXRWJPYSQQZ5Z6T832CSZ"

func NewUUID

func NewUUID() string

NewUUID UUID v4

推荐使用: NewXid or NewULID.

PS: (1) 重复概率非常低,不建议用作分布式唯一id. (2) 格式(5组): {8}-{4}-{4}-{4}-{12} (3) 长度: 36

e.g.

() => "936eff5f-97c6-4f8b-b26d-9bab1f65ff55"

func NewXid

func NewXid() string

NewXid Xid是一个全局唯一的ID生成器,它使用Mongo Object ID算法来生成全局唯一的ID.

极小概率导致重复的情况: (1) 时钟回拨(存疑?); (2) 不同机器的机器ID碰巧相同.

@return (1) 长度固定为20

(2) 组成: 小写字母(a-f)、数字(0-9)

e.g.

"ckic7hfnl531vbl645n0"
"ckth51co47mgs2kacmk0"

func NewXidWithTime

func NewXidWithTime(t time.Time) string

Types

This section is empty.

Jump to

Keyboard shortcuts

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