10 examples of 'golang generate uuid' in Go

Every line of 'golang generate uuid' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Go code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
26func createUUID() (uuid string) {
27 u := new([16]byte)
28 _, err := rand.Read(u[:])
29 danger("Cannot generate UUID", err)
30 // 0x40 is reserved variant from RFC 4122
31 u[8] = (u[8] | 0x40) & 0x7F
32 // Set the four most significant bits (bits 12 through 15) of the
33 // time_hi_and_version field to the 4-bit version number.
34 u[6] = (u[6] & 0xF) | (0x4 << 4)
35 uuid = fmt.Sprintf("%x-%x-%x-%x-%x", u[0:4], u[4:6], u[6:8], u[8:10], u[10:])
36 return
37}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

73func NewRandomUUID() (UUID, error) {
74 var ru UUID
75
76 r, err := randomBase()
77 if err != nil {
78 return ZeroUUID, err
79 }
80 r[6] = (r[6] & 0x0F) | 0x40
81 r[8] = (r[8] &^ 0x40) | 0x80
82
83 unmarshalUUID(r, BytesType, &ru)
84 return ru, nil
85}
106func (app *Application) GenerateUUID(uuidVal string) string {
107 base := app.config.UUID
108 if len(uuidVal) == 8 {
109 base = ""
110 }
111 return base + uuidVal + app.config.UUIDSuffix
112}
8func UUID() string {
9 b := make([]byte, 16)
10 rand.Read(b)
11 return fmt.Sprintf("%x-%x-%x-%x-%x", b[0:4], b[4:6], b[6:8], b[8:10], b[10:])
12}
24func uuid() string {
25 b := make([]byte, 16)
26 crand.Read(b)
27 return fmt.Sprintf("%x-%x-%x-%x-%x", b[0:4], b[4:6], b[6:8], b[8:10], b[10:])
28}
38func NewUUID() ([]byte, error) {
39 // TODO: V4 UUIDs are randomly generated. It would be more efficient to instead
40 // use time-based UUIDs so the prefixes of the UUIDs are similar. V1 UUIDs use
41 // the current timestamp and the server's MAC address but the latter isn't
42 // guaranteed to be unique since we may have multiple processes running on the
43 // same host. Elasticsearch uses Flake IDs which ensure uniqueness by requiring
44 // an initial coordination step and we may want to consider doing the same.
45 uuid := uuid.NewRandom()
46
47 buf := make([]byte, encodedLen)
48 base64.StdEncoding.Encode(buf, uuid)
49 return buf, nil
50}
64func createUUID() (string, error) {
65 u, err := uuid.NewUUID()
66 if err != nil {
67 return "", err
68 }
69 result := u.String()
70 result = strings.Replace(result, "-", "", -1)
71 return result, nil
72}
104func UUID() string {
105 n := 10
106 b := make([]byte, n)
107 // A src.Int63() generates 63 random bits, enough for letterIdxMax characters!
108 for i, cache, remain := n-1, src.Int63(), letterIdxMax; i >= 0; {
109 if remain == 0 {
110 cache, remain = src.Int63(), letterIdxMax
111 }
112 if idx := int(cache & letterIdxMask); idx < len(letterBytes) {
113 b[i] = letterBytes[idx]
114 i--
115 }
116 cache >>= letterIdxBits
117 remain--
118 }
119
120 return string(b)
121}
224func GenerateLenUUID(i int) (string, error) {
225 if i < 0 {
226 i = 2<<29 + i
227 }
228 return FormatUUID(fmt.Sprintf("%x", i))
229}
28func (h Helpers) UUID() string {
29 return uuid.V4().String()
30}

Related snippets