GolangNote

Golang笔记

golang 生成良好的唯一ID/uuid库比较

Permalink

下表是生成uuid 库的比较,可以根据实际需求选择相应的库

package id format
github.com/segmentio/ksuid 0pPKHjWprnVxGH7dEsAoXX2YQvU 4 bytes of time (seconds) + 16 random bytes
github.com/rs/xid b50vl5e54p1000fo3gh0 4 bytes of time (seconds) + 3 byte machine id + 2 byte process id + 3 bytes random
github.com/kjk/betterguid -Kmdih_fs4ZZccpx2Hl1 8 bytes of time (milliseconds) + 9 random bytes
github.com/sony/sonyflake 20f8707d6000108 ~6 bytes of time (10 ms) + 1 byte sequence + 2 bytes machine id
github.com/oklog/ulid 01BJMVNPBBZC3E36FJTGVF0C4S 6 bytes of time (milliseconds) + 8 bytes random
github.com/chilts/sid 1JADkqpWxPx-4qaWY47~FqI 8 bytes of time (ns) + 8 random bytes
github.com/satori/go.uuid 5b52d72c-82b3-4f8e-beb5-437a974842c UUIDv4 from RFC 4112 for comparison

另外一些库

  • nu7hatch/gouuid - This package provides immutable UUID structs and the functions NewV3, NewV4, NewV5 and Parse() for generating versions 3, 4 and 5 UUIDs as specified in RFC 4122.
  • pborman/uuid - The uuid package generates and inspects UUIDs based on RFC 4122 and DCE 1.1: Authentication and Security Services.
  • goid - Generate and Parse RFC4122 compliant V4 UUIDs.
  • uuid - Generate, encode, and decode UUIDs v1 with fast or cryptographic-quality random node identifier.

基于时间的 uuid 例子

Go: uuid
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package main

 import (
 	"crypto/rand"
 	"fmt"
 	"time"
 )

 func main() {
 	// generate 32 bits timestamp
 	unix32bits := uint32(time.Now().UTC().Unix())

 	buff := make([]byte, 12)

 	numRead, err := rand.Read(buff)

 	if numRead != len(buff) || err != nil {
 		panic(err)
 	}

 	fmt.Printf("%x-%x-%x-%x-%x-%x\n", unix32bits, buff[0:2], buff[2:4], buff[4:6], buff[6:8], buff[8:])
 }

输出:

plaintext: output
1
59658c4d-5b7e-bd35-b887-c6f4-41bcc1be

利用linux 系统命令生成 /usr/bin/uuidgen,但这种方式比较慢, 可用 nu7hatch/gouuidsatori/go.uuid

Go: uuidgen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package main

import (
    "fmt"
    "log"
    "os/exec"
)

func main() {
    out, err := exec.Command("uuidgen").Output()
    if err != nil {
        log.Fatal(err)
    }
    fmt.Printf("%s", out)
}

输出:

plaintext: output
1
FEDDB383-F25D-4F08-A40C-FF7D587417C3

一般场景推荐使用 https://github.com/rs/xid,其性能好、字符串较短,特性如下:

plaintext: rs/xid
1
2
3
4
5
6
7
Size: 12 bytes (96 bits), smaller than UUID, larger than snowflake
Base32 hex encoded by default (20 chars when transported as printable string, still sortable)
Non configured, you don't need set a unique machine and/or data center id
K-ordered
Embedded time with 1 second precision
Unicity guaranteed for 16,777,216 (24 bits) unique ids per second and per host/process
Lock-free (i.e.: unlike UUIDv1 and v2)

Xid 取决于系统时间,是个递增的计数器,因此不具有加密安全性。 如果你的应用里 ID 的不可预测性很重要,则不应使用 Xid 。 值得注意的是,大多数其他类似 UUID 的实现也不具有加密安全性。 如果你想要一个真正的随机 ID 生成器,你应该使用依赖于加密安全源的库(如 unix 上的 /dev/urandom,golang中的 crypto/rand )。

本文网址: https://golangnote.com/topic/177.html 转摘请注明来源

Related articles

Golang 生成防识别的图片验证码

验证码 captcha 是对抗密码强力破解、垃圾信息的有效方式,一般用于用户注册、登录,当检测到频繁发帖时也会启用验证码。下面介绍用golang 生成防机器识别的图片验证码。...

Write a Comment to "golang 生成良好的唯一ID/uuid库比较"

Submit Comment Login
Based on Golang + fastHTTP + sdb | go1.20 Processed in 0ms