Is there a method to generate a UUID with go language

后端 未结 12 1714
温柔的废话
温柔的废话 2020-12-07 12:15

I have code that looks like this:

u := make([]byte, 16)
_, err := rand.Read(u)
if err != nil {
    return
}

u[8] = (u[8] | 0x80) & 0xBF // what does thi         


        
相关标签:
12条回答
  • 2020-12-07 12:23

    This library is our standard for uuid generation and parsing:

    https://github.com/pborman/uuid

    0 讨论(0)
  • 2020-12-07 12:24

    gofrs/uuid is the replacement for satori/go.uuid, which is the most starred UUID package for Go. It supports UUID versions 1-5 and is RFC 4122 and DCE 1.1 compliant.

    import "github.com/gofrs/uuid"
    
    // Create a Version 4 UUID, panicking on error
    u := uuid.Must(uuid.NewV4())
    
    0 讨论(0)
  • 2020-12-07 12:28

    On Linux, you can read from /proc/sys/kernel/random/uuid:

    package main
    
    import "io/ioutil"
    import "fmt"
    
    func main() {
        u, _ := ioutil.ReadFile("/proc/sys/kernel/random/uuid")
        fmt.Println(string(u))
    }
    

    No external dependencies!

    $ go run uuid.go 
    3ee995e3-0c96-4e30-ac1e-f7f04fd03e44
    
    0 讨论(0)
  • 2020-12-07 12:30

    You can generate UUIDs using the go-uuid library. This can be installed with:

    go get github.com/nu7hatch/gouuid
    

    You can generate random (version 4) UUIDs with:

    import "github.com/nu7hatch/gouuid"
    
    ...
    
    u, err := uuid.NewV4()
    

    The returned UUID type is a 16 byte array, so you can retrieve the binary value easily. It also provides the standard hex string representation via its String() method.

    The code you have also looks like it will also generate a valid version 4 UUID: the bitwise manipulation you perform at the end set the version and variant fields of the UUID to correctly identify it as version 4. This is done to distinguish random UUIDs from ones generated via other algorithms (e.g. version 1 UUIDs based on your MAC address and time).

    0 讨论(0)
  • 2020-12-07 12:33

    For Windows, I did recently this:

    // +build windows
    
    package main
    
    import (
        "syscall"
        "unsafe"
    )
    
    var (
        modrpcrt4 = syscall.NewLazyDLL("rpcrt4.dll")
        procUuidCreate = modrpcrt4.NewProc("UuidCreate")
    )
    
    const (
        RPC_S_OK = 0
    )
    
    func NewUuid() ([]byte, error) {
        var uuid [16]byte
        rc, _, e := syscall.Syscall(procUuidCreate.Addr(), 1,
                 uintptr(unsafe.Pointer(&uuid[0])), 0, 0)
        if int(rc) != RPC_S_OK {
            if e != 0 {
                return nil, error(e)
            } else {
                return nil, syscall.EINVAL
            }
        }
        return uuid[:], nil
    }
    
    0 讨论(0)
  • 2020-12-07 12:38

    As part of the uuid spec, if you generate a uuid from random it must contain a "4" as the 13th character and a "8", "9", "a", or "b" in the 17th (source).

    // this makes sure that the 13th character is "4"
    u[6] = (u[6] | 0x40) & 0x4F
    // this makes sure that the 17th is "8", "9", "a", or "b"
    u[8] = (u[8] | 0x80) & 0xBF 
    
    0 讨论(0)
提交回复
热议问题