2026-04-25 01:18:30 +08:00

19 lines
392 B
Go

package idgen
import (
"crypto/rand"
"encoding/hex"
"fmt"
"time"
)
// New 返回带时间前缀的随机 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[:]))
}