Skip to content

Instantly share code, notes, and snippets.

@holys
Last active August 29, 2015 14:07
Show Gist options
  • Save holys/2c574e173542c51f2c56 to your computer and use it in GitHub Desktop.
Save holys/2c574e173542c51f2c56 to your computer and use it in GitHub Desktop.
package main
import (
"crypto/rand"
"fmt"
)
type UUID [16]byte
func NewUUID() *UUID {
u := &UUID{}
_, err := rand.Read(u[:16])
if err != nil {
panic(err) // TODO: panic??
}
u[8] = (u[8] | 0x80) & 0xBf
u[6] = (u[6] | 0x40) & 0x4f
return u
}
func (self *UUID) String() string {
return fmt.Sprintf("%x-%x-%x-%x-%x", self[:4], self[4:6], self[6:8], self[8:10], self[10:])
}
func (self *UUID) HexString() string {
return fmt.Sprintf("%x%x%x%x%x", self[:4], self[4:6], self[6:8], self[8:10], self[10:])
}
func main() {
uuid := NewUUID()
fmt.Println(uuid.HexString())
fmt.Println(NewUUID().HexString())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment