-
Notifications
You must be signed in to change notification settings - Fork 5
/
5.5-snowflake.go
55 lines (52 loc) · 1.21 KB
/
5.5-snowflake.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
//++++++++++++++++++++++++++++++++++++++++
// 《Go语言高级开发与实战》源码
//++++++++++++++++++++++++++++++++++++++++
// Author:廖显东(ShirDon)
// Blog:https://www.shirdon.com/
// 知乎:https://www.zhihu.com/people/shirdonl
// 公众号:源码大数据
// 仓库地址:https://gitee.com/shirdonl/goAdvanced
// 仓库地址:https://github.com/shirdonl/goAdvanced
//++++++++++++++++++++++++++++++++++++++++
package main
import (
"fmt"
"github.com/GUAIK-ORG/go-snowflake/snowflake"
"github.com/golang/glog"
"sync"
"time"
)
func main() {
var wg sync.WaitGroup
// 创建Snowflake对象
s, err := snowflake.NewSnowflake(int64(1), int64(1))
if err != nil {
glog.Error(err)
return
}
var check sync.Map
t1 := time.Now()
for i := 0; i < 100; i++ {
wg.Add(1)
go func() {
defer wg.Done()
//生成唯一ID
val := s.NextVal()
//打印生成的id
fmt.Println(val)
if _, ok := check.Load(val); ok {
// id冲突检查
glog.Error(fmt.Errorf("error#unique: val:%v", val))
return
}
check.Store(val, 0)
if val == 0 {
glog.Error(fmt.Errorf("error"))
return
}
}()
}
wg.Wait()
elapsed := time.Since(t1)
glog.Infof("generate 20k ids elapsed: %v", elapsed)
}