idgen.go
674 字节
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
package idgen
import (
"fmt"
"github.com/sony/sonyflake"
)
var sf *sonyflake.Sonyflake
func init() {
st := sonyflake.Settings{
MachineID: getMachineId,
}
sf = sonyflake.NewSonyflake(st)
}
func getMachineId() (uint16, error) {
// TODO
return 1, nil
}
// Next generates next id as an uint64
func Next() (id int64) {
var i uint64
if sf != nil {
i, _ = sf.NextID()
id = int64(i)
}
return
}
// NextString generates next id as a string
func NextString() (id string) {
id = fmt.Sprintf("%d", Next())
return
}
func GetOne() int64 {
return Next()
}
func GetMulti(n int) (ids []int64) {
for i := 0; i < n; i++ {
ids = append(ids, Next())
}
return
}