From d51ab3304edab407997eee028163fe72e395d922 Mon Sep 17 00:00:00 2001 From: finlab Date: Sun, 27 Dec 2020 16:38:16 +0800 Subject: [PATCH] add example --- example/go.mod | 5 +++++ example/go.sum | 2 ++ example/main.go | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+) create mode 100644 example/go.mod create mode 100644 example/go.sum create mode 100644 example/main.go diff --git a/example/go.mod b/example/go.mod new file mode 100644 index 0000000..a132830 --- /dev/null +++ b/example/go.mod @@ -0,0 +1,5 @@ +module esin.io/finlab/demo + +go 1.15 + +require esin.io/finlab/snowflake v0.0.0-20201227082138-bb688a618b0e // indirect diff --git a/example/go.sum b/example/go.sum new file mode 100644 index 0000000..471d317 --- /dev/null +++ b/example/go.sum @@ -0,0 +1,2 @@ +esin.io/finlab/snowflake v0.0.0-20201227082138-bb688a618b0e h1:/FUv+dmC0G1+ftZdT8r4Vg5ZV2Udx9BFdkDRNG+V8K0= +esin.io/finlab/snowflake v0.0.0-20201227082138-bb688a618b0e/go.mod h1:lFosyt/rdezHRPTLulKiH1MwfSCwaPD40ZcdSBKzy/4= diff --git a/example/main.go b/example/main.go new file mode 100644 index 0000000..fe5929b --- /dev/null +++ b/example/main.go @@ -0,0 +1,38 @@ +package main + +import ( + "fmt" + + "esin.io/finlab/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. + 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()) +}