first commit
This commit is contained in:
31
mengyaping-backend/utils/id.go
Normal file
31
mengyaping-backend/utils/id.go
Normal file
@@ -0,0 +1,31 @@
|
||||
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)
|
||||
}
|
||||
Reference in New Issue
Block a user