7 Commits

Author SHA1 Message Date
Godruoyi
0500e4b57b Merge branch 'master' of https://github.com/godruoyi/go-snowflake 2021-11-26 13:54:13 +08:00
Godruoyi
937cb414e9 👒 fix timezone to utc and perfect example demo 2021-11-26 13:54:11 +08:00
徐连波
495d927cce Merge pull request #1 from 0xflotus/patch-1
fix: small typo
2021-04-16 17:37:46 +08:00
0xflotus
03bb06070a fix: small typo 2021-04-16 11:19:54 +02:00
徐连波
20079db81e Create LICENSE 2021-04-15 17:58:18 +08:00
Godruoyi
056fce86b8 remove test for golangci-lint unpass 2021-04-15 15:48:06 +08:00
Godruoyi
ad2188ad52 🔔 Add funding.yml 2021-04-15 15:46:26 +08:00
6 changed files with 32 additions and 10 deletions

2
.github/FUNDING.yml vendored Normal file
View File

@@ -0,0 +1,2 @@
# These are supported funding model platforms
custom: ["https://images.godruoyi.com/wechat.png"]

21
LICENSE Normal file
View File

@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2021 Godruoyi
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@@ -2,13 +2,18 @@ package main
import (
"fmt"
"time"
"github.com/godruoyi/go-snowflake"
)
func main() {
// set starttime and machineID for the first time if you wan't to use the default value
snowflake.SetStartTime(time.Date(2021, 9, 1, 0, 0, 0, 0, time.UTC))
snowflake.SetMachineID(snowflake.PrivateIPToMachineID()) // testing, not to be used in production
id := snowflake.ID()
fmt.Println(id)
fmt.Println(id) // 1537200202186752
sid := snowflake.ParseID(id)
// SID {

View File

@@ -36,7 +36,7 @@ So if you want use the snowflake algorithm to generate unique ID, You must ensur
Based on this, we created this package and integrated multiple sequence-number providers into it.
* AtomicResolver (base sync/atmoic)
* AtomicResolver (base sync/atomic)
> Each provider only needs to ensure that the serial number generated in the same millisecond is different. You can get a unique ID.
@@ -54,7 +54,7 @@ Based on this, we created this package and integrated multiple sequence-number p
$ go get github.com/godruoyi/go-snowflake
```
## Useage
## Usage
1. simple to use.

View File

@@ -84,7 +84,7 @@ func SetStartTime(s time.Time) {
panic("The start time cannot be a zero value")
}
if s.After(time.Now()) {
if s.After(time.Now().UTC()) {
panic("The s cannot be greater than the current millisecond")
}

View File

@@ -16,12 +16,6 @@ func TestID(t *testing.T) {
t.Error("The snowflake should't < 0.")
}
id2 := 1644633515267981312
df := 392111185853
if id2 != ((df << (snowflake.MachineIDLength + snowflake.SequenceLength)) | 0 | 0) {
t.Error("Create snowflake should be equal 1644633515267981312")
}
mp := make(map[uint64]bool)
for i := 0; i < 100000; i++ {
id, e := snowflake.NextID()