Go语言学习
  • README
  • Go 基础
    • go语言介绍
    • go语言主要特性
    • go内置类型和函数
    • init函数和main函数
    • 下划线
    • iota
    • 字符串
    • 数据类型:数组与切片
    • 数据类型:byte、rune与字符串
    • 变量的5种创建方式
    • 数据类型:字典
    • 指针
    • 数据类型:指针
    • 类型断言
    • 流程控制:defer延迟执行
    • 异常机制:panic和recover
    • 函数
    • go依赖管理
    • go中值传递、引用传递、指针传递区别
  • 标准库
    • Go net/http包
  • 数据结构
    • 哈希表
      • 为什么对 map 的 key 或 value 进行取址操作是不允许的
  • Gin
    • gin 快速开始
    • gin-swagger用法
  • Go 进阶
    • Go 指针
    • Go 中的 GC 演变是怎样的?
    • Go 的堆和栈
  • 面向对象
    • make 和 new 的区别
    • new(T) 和 &T{} 有什么区别?
  • 并发编程
    • Channel
    • Go语言 CSP 并发模型
    • GMP 模型原理
      • GMP 模型为什么要有 P ?
    • Go 协程池(goroutine pool)
    • Go语言常见的并发模式
    • Go并发实践:主动停止goroutine
  • 最佳实践
    • 发布Go语言模块
  • 软件包
    • 常用的GoLang包工具
    • Go的UUID生成
    • 现代化的命令行框架Cobra
    • 配置解析神器Viper
    • Go发送邮件gomail
    • Go反射框架Fx
    • NSQ消息队列的使用
    • Go爬虫框架colly
    • grpc-go 的安装和使用
Powered by GitBook
On this page
  • go软件包列表
  • 代码

Was this helpful?

  1. 软件包

Go的UUID生成

go软件包列表

软件包
样例
格式

0pPKHjWprnVxGH7dEsAoXX2YQvU

4 bytes of time (seconds) + 16 random bytes

b50vl5e54p1000fo3gh0

4 bytes of time (seconds) + 3 byte machine id + 2 byte process id + 3 bytes random

-N-35bz_wVbhxzTZD2wO

8 bytes of time (milliseconds) + 9 random bytes

402329004549341446

~6 bytes of time (10 ms) + 1 byte sequence + 2 bytes machine id

1512052461246971904

1 Bit Unused + 41 Bit Timestamp + 10 Bit NodeID + 12 Bit Sequence ID

1512052461246971904

1 Bit Unused + 41 Bit Timestamp + 10 Bit NodeID + 12 Bit Sequence ID

01BJMVNPBBZC3E36FJTGVF0C4S

6 bytes of time (milliseconds) + 8 bytes random

1JADkqpWxPx-4qaWY47~FqI

8 bytes of time (ns) + 8 random bytes

dwRQAc68PhHQh4BUnrNsoS

UUIDv4 or v5, encoded in a more compact way

5b52d72c-82b3-4f8e-beb5-437a974842c

UUIDv4 from RFC 4112 for comparison

dd5f48eb-1722-4e5f-9d56-dcaf0aae1026

UUIDv4

330806cf-684c-43f7-85aa-79939ed3415d

UUIDv4

624ee0fb37583f00042dc309

4-byte timestamp + 5-byte random value + 3-byte incrementing counter

代码

package main

import (
    "fmt"
    "log"
    "math/rand"
    "time"

    "github.com/bwmarrin/snowflake"
    "github.com/chilts/sid"
    goSnowflake "github.com/godruoyi/go-snowflake"
    guuid "github.com/google/uuid"
    "github.com/kjk/betterguid"
    "github.com/lithammer/shortuuid"
    "github.com/oklog/ulid"
    pborman "github.com/pborman/uuid"
    "github.com/rs/xid"
    goUUID "github.com/satori/go.uuid"
    "github.com/segmentio/ksuid"
    "github.com/sony/sonyflake"
    "go.mongodb.org/mongo-driver/bson/primitive"
)

func genShortUUID() {
    id := shortuuid.New()
    fmt.Printf("github.com/lithammer/shortuuid: %s\n", id)
}

func genXid() {
    id := xid.New()
    fmt.Printf("github.com/rs/xid:              %s\n", id.String())
}

func genKsuid() {
    id := ksuid.New()
    fmt.Printf("github.com/segmentio/ksuid: %s\n", id.String())
}

func genBetterGUID() {
    id := betterguid.New()
    fmt.Printf("github.com/kjk/betterguid: %s\n", id)
}

func genUlid() {
    t := time.Now().UTC()
    entropy := rand.New(rand.NewSource(t.UnixNano()))
    id := ulid.MustNew(ulid.Timestamp(t), entropy)
    fmt.Printf("github.com/oklog/ulid: %s\n", id.String())
}

func genSonyflake() {
    flake := sonyflake.NewSonyflake(sonyflake.Settings{})
    id, err := flake.NextID()
    if err != nil {
        log.Fatalf("flake.NextID() failed with %s\n", err)
    }
    // Note: this is base16, could shorten by encoding as base62 string
    fmt.Printf("github.com/sony/sonyflake: %d\n", id)
}

func genSnowflake() {
    nodeId := rand.Int63() % 1023
    node, err := snowflake.NewNode(nodeId)
    if err != nil {
        log.Fatalf("snowflake.NewNode() failed with %s\n", err)
    }

    id := node.Generate().Int64()
    fmt.Printf("github.com/bwmarrin/snowflake: %d\n", id)
}

func genGoSnowflake() {
    id := goSnowflake.ID()
    fmt.Printf("github.com/godruoyi/go-snowflake: %d\n", id)
}

func genSid() {
    id := sid.Id()
    fmt.Printf("github.com/chilts/sid: %s\n", id)
}

func genUUIDv4() {
    id := goUUID.NewV4()
    fmt.Printf("github.com/satori/go.uuid: %s\n", id)
}

func genUUID() {
    id := guuid.New()
    fmt.Printf("github.com/google/uuid: %s\n", id.String())
}

func genPbormanUUID() {
    id := pborman.NewRandom()
    fmt.Printf("github.com/pborman/uuid: %s\n", id.String())
}

func genMongoDBObjectID() {
    id := primitive.NewObjectID()
    fmt.Printf("go.mongodb.org/mongo-driver/bson/primitive: %s\n", id.Hex())
}

func main() {
    genXid()
    genKsuid()
    genBetterGUID()
    genUlid()
    genSonyflake()
    genSnowflake()
    genGoSnowflake()
    genSid()
    genShortUUID()
    genUUIDv4()
    genUUID()
    genPbormanUUID()
    genMongoDBObjectID()
}
Previous常用的GoLang包工具Next现代化的命令行框架Cobra

Last updated 2 years ago

Was this helpful?

ksuid
xid
betterguid
sonyflake
snowflake
snowflake
ulid
sid
shortuuid
go.uuid
google.uuid
bomberman.uuid
MongoDB ObjectID