go uuid


原文链接: go uuid

UUID 在分布式系统中的常用技术
毫秒数在高位,自增序列在低位,整个ID都是趋势递增的。

  1. 针对 ID是否可预测
    有序
    无序
    Snowflake
  2. 时钟回拨



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

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

https://github.com/nu7hatch/gouuid 7 - 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.
https://github.com/pborman/uuid 8 - The uuid package generates and inspects UUIDs based on RFC 4122 and DCE 1.1: Authentication and Security Services.
https://github.com/jakehl/goid 12 - Generate and Parse RFC4122 compliant V4 UUIDs.
https://github.com/agext/uuid 15 - Generate, encode, and decode UUIDs v1 with fast or cryptographic-quality random node identifier.
基于时间的uuid 例子

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:])
 }

输出:

59658c4d-5b7e-bd35-b887-c6f4-41bcc1be
利用linux 系统命令生成 /usr/bin/uuidgen,但这种方式比较慢, 可用 nu7hatch/gouuid 或 satori/go.uuid

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)
}

输出:

FEDDB383-F25D-4F08-A40C-FF7D587417C3
一般场景推荐使用 https://github.com/rs/xid 136,其性能好、字符串较短,特性如下:

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)。

Relative Articles
golang 使用crypto/rand 生成随机数
Golang 生成 session id

`