32 lines
644 B
Go
32 lines
644 B
Go
package utils
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"encoding/hex"
|
|
"time"
|
|
)
|
|
|
|
// GenerateID 生成唯一ID
|
|
func GenerateID() string {
|
|
timestamp := time.Now().UnixNano()
|
|
randomBytes := make([]byte, 4)
|
|
rand.Read(randomBytes)
|
|
return hex.EncodeToString([]byte{
|
|
byte(timestamp >> 56),
|
|
byte(timestamp >> 48),
|
|
byte(timestamp >> 40),
|
|
byte(timestamp >> 32),
|
|
byte(timestamp >> 24),
|
|
byte(timestamp >> 16),
|
|
byte(timestamp >> 8),
|
|
byte(timestamp),
|
|
}) + hex.EncodeToString(randomBytes)
|
|
}
|
|
|
|
// GenerateShortID 生成短ID
|
|
func GenerateShortID() string {
|
|
randomBytes := make([]byte, 6)
|
|
rand.Read(randomBytes)
|
|
return hex.EncodeToString(randomBytes)
|
|
}
|