// 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. 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()) }
输出:
Int64 ID: 1628304129979846656 String ID: 1628304129979846656 Base2 ID: 1011010011000111001011111101010100101110000000001000000000000 Base64 ID: MTYyODMwNDEyOTk3OTg0NjY1Ng== ID Time : 1677052931672 ID Node : 1 ID Step : 0 ID : 1628304129979846657
UUID Demo:
package main
import ( "fmt"
"github.com/satori/go.uuid" )
funcmain() { // Creating UUID Version 4 // panic on error u1 := uuid.Must(uuid.NewV4(), nil) fmt.Printf("UUIDv4: %s\n", u1)
// or error handling u2 := uuid.NewV4() fmt.Printf("UUIDv4: %s\n", u2)
// Parsing UUID from string input u2, err := uuid.FromString("6ba7b810-9dad-11d1-80b4-00c04fd430c8") if err != nil { fmt.Printf("Something went wrong: %s", err) return } fmt.Printf("Successfully parsed: %s", u2) }