Files
zogo/snowid/README.md
T

28 lines
954 B
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# snowid
基于 `bwmarrin/snowflake` 的雪花 ID 生成器(进程内单例)+ GORM 模型 ID 钩子模板。
## 用法
```go
import "git.zeroonesoft.cn/golib/zogo/snowid"
func main() {
if err := snowid.Init(1); err != nil { ... } // 节点ID 0-1023
id := snowid.Generate() // int64
s := snowid.GenerateString() // string
}
// 生成模型(gorm.io/gen):让 bigint 主键自动填充
// gen-model 脚本里:
opts = append(opts, gen.WithMethod(snowid.IDMethod{}))
// 生成的模型自带 BeforeCreate:Id==0 时填 snowid.Generate()
```
## 注意
- **必须先 Init 再 Generate**,否则 panic(启动期暴露,不留隐患)。
- `InitWithStartTime` 会改全局 Epoch,须在进程首个 NewNode 前调用;同一部署里
各服务的 Epoch 口径要一致,ID 才可排序比较。
- 多实例部署时节点 ID 不得重复,否则会生成重复 ID。