- /*
-
- Copyright (c) [2023] [pcm]
- [pcm-coordinator] is licensed under Mulan PSL v2.
- You can use this software according to the terms and conditions of the Mulan PSL v2.
- You may obtain a copy of Mulan PSL v2 at:
- http://license.coscl.org.cn/MulanPSL2
- THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
- EITHER EXPaRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
- MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
- See the Mulan PSL v2 for more details.
-
- */
-
- package utils
-
- import (
- "github.com/bwmarrin/snowflake"
- "github.com/zeromicro/go-zero/core/logx"
- )
-
- var node *snowflake.Node
-
- func InitSnowflake(machineID int64) (err error) {
- node, err = snowflake.NewNode(machineID)
- if err != nil {
- logx.Errorf("snowflake Init error : %s", err)
- return
- }
-
- return
- }
-
- // GenSnowflakeID 生成雪花算法id
- // machineId 工作id
- func GenSnowflakeID() int64 {
- return node.Generate().Int64()
- }
-
- // GenSnowflakeIDStr 工作id
- func GenSnowflakeIDStr() string {
- return node.Generate().String()
- }
-
- // GenSnowflakeIDStr 工作id
- func GenSnowflakeIDUint() uint {
- return uint(node.Generate().Int64())
- }
|