39 lines
883 B
Go
39 lines
883 B
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"math/rand"
|
|
"time"
|
|
)
|
|
|
|
// Base-62 encoding characters
|
|
const base62 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
|
|
|
|
// Convert a number to Base-62
|
|
func toBase62(num int64) string {
|
|
var encoded string
|
|
for num > 0 {
|
|
remainder := num % 62
|
|
encoded = string(base62[remainder]) + encoded
|
|
num /= 62
|
|
}
|
|
return encoded
|
|
}
|
|
|
|
func generateID() string {
|
|
epochStart := time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC).UnixMilli()
|
|
now := time.Now().UTC().UnixMilli()
|
|
timestamp := now - epochStart // Get milliseconds since 2020
|
|
timestamp %= 1_000_000_000_000 // Keep only 12 digits
|
|
|
|
randomNum := rand.Intn(100) // Random 2-digit number (00-99)
|
|
finalNum := timestamp*100 + int64(randomNum)
|
|
|
|
return fmt.Sprintf("%08s", toBase62(finalNum)) // Ensure 8-char output
|
|
}
|
|
|
|
func main() {
|
|
fmt.Println(generateID()) // Example output: "A1b2C3D4"
|
|
}
|
|
|