2026-05-02 13:02:38 +08:00

20 lines
474 B
Go

// Package idgen 提供后台服务内部使用的轻量随机 ID。
package idgen
import (
"crypto/rand"
"encoding/hex"
"fmt"
"time"
)
// New 返回带时间前缀的随机 ID。
// 后台上传对象使用该 ID 作为文件名片段,时间前缀方便排查对象生成时刻。
func New(prefix string) string {
var entropy [8]byte
_, _ = rand.Read(entropy[:])
return fmt.Sprintf("%s_%d_%s", prefix, time.Now().UnixMilli(), hex.EncodeToString(entropy[:]))
}