Files
go_blog/pkg/util/uuid.go
2025-06-26 17:46:08 +08:00

24 lines
537 B
Go
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.
package util
import (
"fmt"
"time"
"github.com/sony/sonyflake"
)
var t = time.Unix(1594909810, 0) // 基准时间
var flake = sonyflake.NewSonyflake(sonyflake.Settings{
StartTime: t, // 必须设置基准时间否则Sonyflake无法正常初始化
})
func GenSonyFlakeId() uint64 {
uuid, err := flake.NextID()
if err != nil {
fmt.Printf("生成SonyFlake ID失败: %v\n", err) // 明确错误信息
return 0 // 返回0表示生成失败根据业务需求可调整
}
return uuid
}