Skip to content

Instantly share code, notes, and snippets.

@ericchiang
Last active August 29, 2015 14:12
Show Gist options
  • Save ericchiang/8f002153372ae7af71b7 to your computer and use it in GitHub Desktop.
Save ericchiang/8f002153372ae7af71b7 to your computer and use it in GitHub Desktop.
uuid
package main
import (
"crypto/rand"
"fmt"
"io"
)
func uuid() (string, error) {
b := make([]byte, 16)
if _, err := io.ReadFull(rand.Reader, b); err != nil {
return "", err
}
b[8] = (b[8] | 0x80) & 0xBF
b[6] = (b[6] | 0x40) & 0x4F
return fmt.Sprintf("%x-%x-%x-%x-%x", b[0:4], b[4:6], b[6:8], b[8:10], b[10:]), nil
}
func main() {
id, err := uuid()
if err == nil {
fmt.Println("My uuid:", id)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment