Add working example
This commit is contained in:
parent
3f3735b65d
commit
b61ac3cab6
43
README.md
43
README.md
@ -17,7 +17,7 @@ This assumes you already have a working Go environment, if not please see
|
|||||||
go get github.com/bwmarrin/flake
|
go get github.com/bwmarrin/flake
|
||||||
```
|
```
|
||||||
|
|
||||||
### Usage
|
### Usage Example
|
||||||
|
|
||||||
Import the package into your project.
|
Import the package into your project.
|
||||||
|
|
||||||
@ -30,8 +30,45 @@ the Generate method to get a unique ID. The only argument to the NewNode()
|
|||||||
method is a Node number. Each node you create must have it's own unique
|
method is a Node number. Each node you create must have it's own unique
|
||||||
Node number. A node number can be any number from 0 to 1023.
|
Node number. A node number can be any number from 0 to 1023.
|
||||||
|
|
||||||
|
** Example Program: **
|
||||||
|
|
||||||
```go
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/bwmarrin/flake"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
|
||||||
|
// Create a new Node with a Node number of 1
|
||||||
node, err := flake.NewNode(1)
|
node, err := flake.NewNode(1)
|
||||||
id := node.Generate()
|
if err != nil {
|
||||||
fmt.Printf("ID: %d, %s\n", id, id.String())
|
fmt.Println(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Generate a snowflake ID.
|
||||||
|
id, err := node.Generate()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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())
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
Loading…
x
Reference in New Issue
Block a user