> For the complete documentation index, see [llms.txt](https://go.codetrue.net/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://go.codetrue.net/packages/go-uuids.md).

# Go的UUID生成

### go软件包列表 <a href="#slide-0" id="slide-0"></a>

| 软件包                                                                                                 | 样例                                   | 格式                                                                                 |
| --------------------------------------------------------------------------------------------------- | ------------------------------------ | ---------------------------------------------------------------------------------- |
| [ksuid](https://developer.aliyun.com/article/github.com/segmentio/ksuid)                            | 0pPKHjWprnVxGH7dEsAoXX2YQvU          | 4 bytes of time (seconds) + 16 random bytes                                        |
| [xid](https://developer.aliyun.com/article/github.com/rs/xid)                                       | b50vl5e54p1000fo3gh0                 | 4 bytes of time (seconds) + 3 byte machine id + 2 byte process id + 3 bytes random |
| [betterguid](https://developer.aliyun.com/article/github.com/kjk/betterguid)                        | -N-35bz\_wVbhxzTZD2wO                | 8 bytes of time (milliseconds) + 9 random bytes                                    |
| [sonyflake](https://developer.aliyun.com/article/github.com/sony/sonyflake)                         | 402329004549341446                   | \~6 bytes of time (10 ms) + 1 byte sequence + 2 bytes machine id                   |
| [snowflake](https://developer.aliyun.com/article/github.com/bwmarrin/snowflake)                     | 1512052461246971904                  | 1 Bit Unused + 41 Bit Timestamp + 10 Bit NodeID + 12 Bit Sequence ID               |
| [snowflake](https://developer.aliyun.com/article/github.com/godruoyi/go-snowflake)                  | 1512052461246971904                  | 1 Bit Unused + 41 Bit Timestamp + 10 Bit NodeID + 12 Bit Sequence ID               |
| [ulid](https://developer.aliyun.com/article/github.com/oklog/ulid)                                  | 01BJMVNPBBZC3E36FJTGVF0C4S           | 6 bytes of time (milliseconds) + 8 bytes random                                    |
| [sid](https://developer.aliyun.com/article/github.com/chilts/sid)                                   | 1JADkqpWxPx-4qaWY47\~FqI             | 8 bytes of time (ns) + 8 random bytes                                              |
| [shortuuid](https://github.com/lithammer/shortuuid)                                                 | dwRQAc68PhHQh4BUnrNsoS               | UUIDv4 or v5, encoded in a more compact way                                        |
| [go.uuid](https://developer.aliyun.com/article/github.com/satori/go.uuid)                           | 5b52d72c-82b3-4f8e-beb5-437a974842c  | UUIDv4 from RFC 4112 for comparison                                                |
| [google.uuid](https://github.com/google/uuid)                                                       | dd5f48eb-1722-4e5f-9d56-dcaf0aae1026 | UUIDv4                                                                             |
| [bomberman.uuid](https://github.com/pborman/uuid)                                                   | 330806cf-684c-43f7-85aa-79939ed3415d | UUIDv4                                                                             |
| [MongoDB ObjectID](https://developer.aliyun.com/article/go.mongodb.org/mongo-driver/bson/primitive) | 624ee0fb37583f00042dc309             | 4-byte timestamp + 5-byte random value + 3-byte incrementing counter               |

### 代码 <a href="#slide-1" id="slide-1"></a>

```go
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()
}
```
