Skip to content

Instantly share code, notes, and snippets.

@lakemove
Created November 14, 2020 07:22
Show Gist options
  • Save lakemove/cf503700eafdce4fee7424cac35fdba9 to your computer and use it in GitHub Desktop.
Save lakemove/cf503700eafdce4fee7424cac35fdba9 to your computer and use it in GitHub Desktop.
golang v4 UUID generator
package main
import (
"crypto/rand"
"encoding/hex"
"fmt"
)
func main() {
id := make([]byte, 16)
rand.Read(id)
id[6] = (id[6] & 0x0f) | 0x40
id[8] = (id[8] & 0x3f) | 0x80
wall := []byte("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")
hex.Encode(wall, id[:4])
hex.Encode(wall[9:13], id[4:6])
hex.Encode(wall[14:18], id[6:8])
hex.Encode(wall[19:23], id[8:10])
hex.Encode(wall[24:], id[10:])
fmt.Println(string(wall))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment