-
Notifications
You must be signed in to change notification settings - Fork 4
/
util.go
47 lines (38 loc) · 917 Bytes
/
util.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
package main
import (
"fmt"
"strconv"
)
// FormatID returns a new 'random' string id
func FormatID(id uint64) string {
return fmt.Sprintf("%03s", strconv.FormatUint(id*46649%6125, 36))
}
// IDGenerator can be used to generate unique IDs
type IDGenerator struct {
index uint64
}
// MakeIDGenerator makes an IDGenerator
func MakeIDGenerator() IDGenerator {
return IDGenerator{
index: 0,
}
}
// UniqID returns and increments the current index
func (g *IDGenerator) UniqID() uint64 {
res := g.index
g.index++
return res
}
// UniqIDf returns a new 'random' string id and increaes the index
func (g *IDGenerator) UniqIDf() string {
return FormatID(g.UniqID())
}
var gen = MakeIDGenerator()
// UniqID returns and increments the current index
func UniqID() uint64 {
return gen.UniqID()
}
// UniqIDf returns a new 'random' string id and increaes the index
func UniqIDf() string {
return gen.UniqIDf()
}