Skip to content

Commit a1e25e1

Browse files
authored
[type:feat] add id utils (#93)
1 parent 6e8ed8d commit a1e25e1

File tree

4 files changed

+103
-0
lines changed

4 files changed

+103
-0
lines changed

core/idutil.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package core
2+
3+
import (
4+
uuid "github.com/satori/go.uuid"
5+
"gopkg.in/mgo.v2/bson"
6+
"strconv"
7+
"strings"
8+
"time"
9+
)
10+
11+
// UnixMillisId generate timestamp id with millisecond.
12+
// @return string
13+
func UnixMillisId() string {
14+
return strconv.FormatInt(time.Now().UnixNano()/1e6, 10)
15+
}
16+
17+
// UnixNanoTimeId generate timestamp id with unixNano.
18+
// @return string
19+
func UnixNanoTimeId() string {
20+
return strconv.FormatInt(time.Now().UnixNano(), 10)
21+
}
22+
23+
// UUIDV1 Version 1, based on timestamp and MAC address (RFC 4122).
24+
// @return string
25+
func UUIDV1() string {
26+
return uuid.NewV1().String()
27+
}
28+
29+
// UUIDV2 Version 2, based on timestamp, MAC address and POSIX UID/GID (DCE 1.1).
30+
// @param domain domain
31+
// @return string uuid string
32+
func UUIDV2(domain byte) string {
33+
return uuid.NewV2(domain).String()
34+
}
35+
36+
// UUIDV3 Version 3, based on MD5 hashing (RFC 4122).
37+
// @param ns uuid namespace
38+
// @param name domain name
39+
// @return string uuid string
40+
func UUIDV3(ns uuid.UUID, name string) string {
41+
return uuid.NewV3(ns, name).String()
42+
}
43+
44+
// UUIDV5 Version 5, based on SHA-1 hashing (RFC 4122).
45+
// @param ns uuid namespace
46+
// @param name domain name
47+
// @return string uuid string
48+
func UUIDV5(ns uuid.UUID, name string) string {
49+
return uuid.NewV5(ns, name).String()
50+
}
51+
52+
// RandomUUID Version 4, based on random numbers (RFC 4122).
53+
// @return string uuid string
54+
func RandomUUID() string {
55+
return uuid.NewV4().String()
56+
}
57+
58+
// SimpleRandomUUID simple RandomUUI base RandomUUID.
59+
// @return string return uuid without horizontal line
60+
func SimpleRandomUUID() string {
61+
return strings.ReplaceAll(RandomUUID(), "-", "")
62+
}
63+
64+
// ObjectId generate mongodb ObjectId, currently, just use mgo.v2 lib, can write self.
65+
// @return string object id
66+
func ObjectId() string {
67+
return bson.NewObjectId().Hex()
68+
}
69+
70+
// ObjectIdWithNow generate ObjectId with now, currently, just use mgo.v2 lib, can write self.
71+
// @return string object id
72+
func ObjectIdWithNow() string {
73+
return bson.NewObjectIdWithTime(time.Now()).Hex()
74+
}

core/idutil_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package core
2+
3+
import (
4+
"github.com/acmestack/godkits/log"
5+
uuid "github.com/satori/go.uuid"
6+
"testing"
7+
)
8+
9+
func TestGenUUIDRandom(t *testing.T) {
10+
log.Info(UnixMillisId())
11+
log.Info(UnixNanoTimeId())
12+
log.Info(UUIDV1())
13+
log.Info(UUIDV2('A'))
14+
log.Info(UUIDV3(uuid.NamespaceOID, "test"))
15+
log.Info(UUIDV5(uuid.NamespaceOID, "test"))
16+
log.Info(RandomUUID())
17+
log.Info(SimpleRandomUUID())
18+
log.Info(ObjectId())
19+
log.Info(ObjectIdWithNow())
20+
}

go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
module github.com/acmestack/godkits
22

33
go 1.16
4+
5+
require (
6+
github.com/satori/go.uuid v1.2.0 // indirect
7+
gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22 // indirect
8+
)

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww=
2+
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
3+
gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22 h1:VpOs+IwYnYBaFnrNAeB8UUWtL3vEUnzSCL1nVjPhqrw=
4+
gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22/go.mod h1:yeKp02qBN3iKW1OzL3MGk2IdtZzaj7SFntXj72NppTA=

0 commit comments

Comments
 (0)