2017-09-29 16:58:48 +00:00
|
|
|
package uuid
|
|
|
|
|
|
|
|
import (
|
2021-01-04 22:06:47 +00:00
|
|
|
"crypto/rand"
|
2017-09-29 16:58:48 +00:00
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
2021-01-04 22:06:47 +00:00
|
|
|
// Generate is used to generate a random UUID.
|
2017-09-29 16:58:48 +00:00
|
|
|
func Generate() string {
|
|
|
|
buf := make([]byte, 16)
|
2021-01-04 22:06:47 +00:00
|
|
|
if _, err := rand.Read(buf); err != nil {
|
2017-09-29 16:58:48 +00:00
|
|
|
panic(fmt.Errorf("failed to read random bytes: %v", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("%08x-%04x-%04x-%04x-%12x",
|
|
|
|
buf[0:4],
|
|
|
|
buf[4:6],
|
|
|
|
buf[6:8],
|
|
|
|
buf[8:10],
|
|
|
|
buf[10:16])
|
|
|
|
}
|
2021-01-04 22:06:47 +00:00
|
|
|
|
|
|
|
// Short is used to generate the first 8 characters of a UUID.
|
|
|
|
func Short() string {
|
|
|
|
return Generate()[0:8]
|
|
|
|
}
|