The recent project used the snowflake algorithm to generate ID, and in the actual running environment will generate duplicate ID, so wrote a go program to verify, by the way also practice the go to the process and channel.
As for the process of go and the basic knowledge of the channel, please own Baidu.
The code is as follows:
Package Mainimport ("FMT" "Github.com/zheng-ji/gosnowflake") func main () {FMT. Println ("Hell Go!") IW, err: = Gosnowflake.newidworker (1) If err! = Nil {fmt. PRINTLN (err)}cons: = Map[int64]int{}chncount: = 10chs: = Make ([]chan Int., Chncount) for J: = 0; J < Chncount; J + + {Chs[j] = make (chan int) go func (idx int) {for i: = 0; i < 10000; i++ {if id, err: = IW. NextID (); Err = = Nil {If _, OK: = Cons[id]; OK {fmt. PRINTLN (idx, " ", id)}}}chs[idx] <-1} (j)}for I: = 0; i < Chncount; i++ {<-chs[i]}}
The results of the operation are as follows:
Goroot=c:\go #gosetupGOPATH =d:\go\testpros;d:\go\testpros\src;d:\go\gopath #gosetupC: \go\bin\go.exe build-i-O D:\ Go\testpros\bin\testpros.exe d:/go/testpros/src/goroutine.go #gosetup "C:\Program Files\jetbrains\goland 2018.1.1\ Bin\runnerw.exe "D:\GO\TestPros\bin\TestPros.exe #gosetupHell GO! Process finished with exit code 0
A total of 10 co-processes were used, each resulting in 1000 IDs, but no duplicate fields were validated by validation. The validation may not be complete, but the reason is not yet found.
GO path instance and validation snowflake algorithm