-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
guid.go
48 lines (42 loc) · 1.31 KB
/
guid.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// guid.go
// description: Generate random globally unique identifiers (GUIDs).
// details:
// A GUID (globally unique identifier) is a 128-bit text string that
// represents an identification (ID). Organizations generate GUIDs when
// a unique reference number is needed to identify information on
// a computer or network. A GUID can be used to ID hardware, software,
// accounts, documents and other items. The term is also often used in
// software created by Microsoft.
// See more information on: https://en.wikipedia.org/wiki/Universally_unique_identifier
// author(s) [cheatsnake](https://github.com/cheatsnake)
// see guid_test.go
// Package guid provides facilities for generating random globally unique identifiers.
package guid
import (
"crypto/rand"
"fmt"
"math/big"
"strings"
)
const pattern string = "xxxxxxxx-xxxx-4xxx-xxxx-xxxxxxxxxxxx"
const versionIndex int = 14
// New returns a randomly generated global unique identifier.
func New() (string, error) {
var guid strings.Builder
for i, ch := range pattern {
if i == versionIndex {
guid.WriteRune(ch)
continue
}
if ch == '-' {
guid.WriteRune(ch)
continue
}
random, err := rand.Int(rand.Reader, big.NewInt(16))
if err != nil {
return "", err
}
guid.WriteString(fmt.Sprintf("%x", random.Int64()))
}
return guid.String(), nil
}