Go 微服务十五 生成雪花ID

538 次浏览次阅读
没有评论

自己写了一个生成雪花 ID 的包,放到了 github 上。地址: jsnowflake。如果喜欢,还望点个 star 谢谢🙏

如有问题,欢迎指出~

此文章基于自己的包实现

安装

go get github.com/jeffcail/jsnowflake 

使用

Demo

package main

import (
    "fmt"
    "github.com/jeffcail/jsnowflake"
)

func main() {m, err := jsnowflake.NewMachine(1)  // 机器 ID
    if err != nil {panic(err)
    }

    for i := 0; i < 100; i++ {id := m.GenerateId()
        fmt.Println(id)
    }
}

结果:

9156990548291096576
9156990548291096577
9156990548291096578
9156990548291096579
9156990548291096580
9156990548291096581
9156990548291096582
9156990548291096583
9156990548291096584
9156990548291096585
9156990548291096586
9156990548291096587
9156990548291096588
9156990548291096589
9156990548291096590
9156990548291096591
9156990548291096592
9156990548291096593
9156990548291096594
9156990548291096595
9156990548291096596
9156990548291096597
9156990548291096598
9156990548291096599
9156990548291096600
9156990548291096601
9156990548291096602
9156990548291096603
9156990548291096604
9156990548291096605
9156990548291096606
9156990548291096607
9156990548291096608
9156990548291096609
9156990548291096610
9156990548291096611
9156990548291096612
9156990548291096613
9156990548291096614
9156990548291096615
9156990548291096616
9156990548291096617
9156990548291096618
9156990548291096619
9156990548291096620
9156990548291096621
9156990548291096622
9156990548291096623
9156990548291096624
9156990548291096625
9156990548291096626
9156990548291096627
9156990548291096628
9156990548291096629
9156990548291096630
9156990548291096631
9156990548291096632
9156990548291096633
9156990548291096634
9156990548291096635
9156990548291096636
9156990548291096637
9156990548291096638
9156990548291096639
9156990548291096640
9156990548291096641
9156990548291096642
9156990548291096643
9156990548291096644
9156990548291096645
9156990548291096646
9156990548291096647
9156990548291096648
9156990548291096649
9156990548291096650
9156990548291096651
9156990548291096652
9156990548291096653
9156990548291096654
9156990548291096655
9156990548291096656
9156990548291096657
9156990548291096658
9156990548291096659
9156990548291096660
9156990548291096661
9156990548291096662
9156990548291096663
9156990548291096664
9156990548291096665
9156990548291096666
9156990548291096667
9156990548291096668
9156990548291096669
9156990548291096670
9156990548291096671
9156990548291096672
9156990548291096673
9156990548295290880
9156990548295290881
正文完
 0
评论(没有评论)