mirror of
https://github.com/godruoyi/go-snowflake.git
synced 2026-01-03 02:45:16 +00:00
🎈 rewrite readme
This commit is contained in:
40
readme.md
40
readme.md
@@ -18,7 +18,7 @@
|
|||||||
|
|
||||||
## Description
|
## Description
|
||||||
|
|
||||||
An Lock Free ID Generator for Golang implementation.
|
An **Lock Free** ID Generator for Golang implementation.
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
@@ -40,6 +40,14 @@ Based on this, we created this package and integrated multiple sequence-number p
|
|||||||
|
|
||||||
> Each provider only needs to ensure that the serial number generated in the same millisecond is different. You can get a unique ID.
|
> Each provider only needs to ensure that the serial number generated in the same millisecond is different. You can get a unique ID.
|
||||||
|
|
||||||
|
## Feature
|
||||||
|
|
||||||
|
- ✅ Lock Free
|
||||||
|
- 🎈 Zero configuration, out of the box
|
||||||
|
- 🚀 Concurrency safety
|
||||||
|
- 🌵 Support private ip to machineid
|
||||||
|
- 🐡 Support custom sequence resolver
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
@@ -51,6 +59,8 @@ $ go get github.com/godruoyi/go-snowflake
|
|||||||
1. simple to use.
|
1. simple to use.
|
||||||
|
|
||||||
```go
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
@@ -67,6 +77,8 @@ func main() {
|
|||||||
2. Specify the MachineID.
|
2. Specify the MachineID.
|
||||||
|
|
||||||
```go
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
@@ -75,6 +87,10 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
snowflake.SetMachineID(1)
|
snowflake.SetMachineID(1)
|
||||||
|
|
||||||
|
// Or set private ip to machineid, testing...
|
||||||
|
// snowflake.SetMachineID(snowflake.PrivateIPToMachineID())
|
||||||
|
|
||||||
id := snowflake.ID()
|
id := snowflake.ID()
|
||||||
fmt.Println(id)
|
fmt.Println(id)
|
||||||
}
|
}
|
||||||
@@ -83,6 +99,8 @@ func main() {
|
|||||||
3. Specify start time.
|
3. Specify start time.
|
||||||
|
|
||||||
```go
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
@@ -100,6 +118,8 @@ func main() {
|
|||||||
4. Parse ID.
|
4. Parse ID.
|
||||||
|
|
||||||
```go
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
@@ -111,11 +131,11 @@ func main() {
|
|||||||
id := snowflake.ID()
|
id := snowflake.ID()
|
||||||
sid := snowflake.ParseID(id)
|
sid := snowflake.ParseID(id)
|
||||||
|
|
||||||
fmt.Println(sid.ID) //
|
fmt.Println(sid.ID) // 132271570944000000
|
||||||
fmt.Println(sid.MachineID) //
|
fmt.Println(sid.MachineID) // 0
|
||||||
fmt.Println(sid.Sequence) //
|
fmt.Println(sid.Sequence) // 0
|
||||||
fmt.Println(sid.Timestamp) //
|
fmt.Println(sid.Timestamp) // 31536000000
|
||||||
fmt.Println(sid.GenerateTime()) //
|
fmt.Println(sid.GenerateTime()) // 2009-11-10 23:00:00 +0000 UTC
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -124,6 +144,8 @@ func main() {
|
|||||||
> ⚠️⚠️ All SetXXX method is thread-unsafe, recommended you call him in the main function.
|
> ⚠️⚠️ All SetXXX method is thread-unsafe, recommended you call him in the main function.
|
||||||
|
|
||||||
```go
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
@@ -148,11 +170,11 @@ func submitOrder(w http.ResponseWriter, req *http.Request) {
|
|||||||
|
|
||||||
## Advanced
|
## Advanced
|
||||||
|
|
||||||
1. Custom sequence resolver.
|
Custom sequence resolver. you can customize the sequence-number resolver by following way:
|
||||||
|
|
||||||
You can customize the sequence-number resolver by following way:
|
|
||||||
|
|
||||||
```go
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|||||||
Reference in New Issue
Block a user