(3)go web开发之 基于雪花算法生成用户ID与自增id,uuid对比

时间:2025-02-22 07:51:37
package main import ( "fmt" "/bwmarrin/snowflake" ) func main() { // Create a new Node with a Node number of 1 // 生成一个节点 node, err := snowflake.NewNode(1) if err != nil fmt.Println(err) return } // Generate a snowflake ID. // type ID int64 这是id的类型,下面定义了很多方法, 获取Base64呀,或者是打印它的时间戳等 id := node.Generate() // Print out the ID in a few different ways. fmt.Printf("Int64 ID: %d\n", id) fmt.Printf("String ID: %s\n", id) fmt.Printf("Base2 ID: %s\n", id.Base2()) fmt.Printf("Base64 ID: %s\n", id.Base64()) // Print out the ID's timestamp fmt.Printf("ID Time : %d\n", id.Time()) // Print out the ID's node number fmt.Printf("ID Node : %d\n", id.Node()) // Print out the ID's sequence number fmt.Printf("ID Step : %d\n", id.Step()) // Generate and print, all in one. fmt.Printf("ID : %d\n", node.Generate().Int64()) }