-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
99 lines (85 loc) · 2.01 KB
/
main.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package main
import (
"fmt"
"github.com/chilts/sid"
"github.com/kjk/betterguid"
"github.com/oklog/ulid"
"github.com/rs/xid"
//"github.com/satori/go.uuid"
"github.com/segmentio/ksuid"
"github.com/sony/sonyflake"
"log"
"math/rand"
"time"
)
func genXid() {
id := xid.New()
fmt.Printf("github.com/rs/xid: %s\n", id.String())
}
func genKsuid() {
id := ksuid.New()
fmt.Printf("github.com/segmentio/ksuid: %s\n", id.String())
}
func genBetterGUID() {
id := betterguid.New()
fmt.Printf("github.com/kjk/betterguid: %s\n", id)
}
func genUlid() {
t := time.Now().UTC()
entropy := rand.New(rand.NewSource(t.UnixNano()))
id := ulid.MustNew(ulid.Timestamp(t), entropy)
fmt.Printf("github.com/oklog/ulid: %s\n", id.String())
}
func getMachineID() (uint16, error) {
var machineID uint16 = 6
return machineID, nil
}
func checkMachineID(machineID uint16) bool {
existsMachines := []uint16{1, 2, 3, 4, 5}
for _, v := range existsMachines {
if v == machineID {
return false
}
}
return true
}
func genSonyflake() {
//st := time.Unix(0,0)
var st time.Time
st, err := time.Parse("2006-01-02", "2014-01-01")
if err != nil {
fmt.Printf("flake.init() failed with %s\n", err.Error())
panic(err)
}
flake := sonyflake.NewSonyflake(sonyflake.Settings{
StartTime: st,
MachineID: getMachineID,
CheckMachineID: checkMachineID,
})
id, err := flake.NextID()
if err != nil {
log.Fatalf("flake.NextID() failed with %s\n", err.Error())
}
fmt.Printf("github.com/sony/sonyflake: %x\n", id)
fmt.Printf("github.com/sony/sonyflake num: %d\n", int64(id))
}
func genSid() {
id := sid.Id()
fmt.Printf("github.com/chilts/sid: %s\n", id)
}
/*func genUUIDv4() {
id, err := uuid.NewV4()
if err != nil {
fmt.Printf("get uuid error [%s]",err)
}
fmt.Printf("github.com/satori/go.uuid: %s\n", id)
}*/
func main() {
genXid()
genKsuid()
genBetterGUID()
genUlid()
genSonyflake()
genSid()
// genUUIDv4()
}