GO path instance and validation snowflake algorithm

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.