uuid.go 455 B

123456789101112131415161718192021222324252627
  1. package feeds
  2. // relevant bits from https://github.com/abneptis/GoUUID/blob/master/uuid.go
  3. import (
  4. "crypto/rand"
  5. "fmt"
  6. )
  7. type UUID [16]byte
  8. // create a new uuid v4
  9. func NewUUID() *UUID {
  10. u := &UUID{}
  11. _, err := rand.Read(u[:16])
  12. if err != nil {
  13. panic(err)
  14. }
  15. u[8] = (u[8] | 0x80) & 0xBf
  16. u[6] = (u[6] | 0x40) & 0x4f
  17. return u
  18. }
  19. func (u *UUID) String() string {
  20. return fmt.Sprintf("%x-%x-%x-%x-%x", u[:4], u[4:6], u[6:8], u[8:10], u[10:])
  21. }