siliconid/siliconid.go

34 lines
788 B
Go

package main
import (
"fmt"
"math/rand"
"time"
)
// Base62 characters
const base62 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
// Converts a number to Base-62
func toBase62(num int64) string {
result := ""
for num > 0 {
result = string(base62[num%62]) + result
num /= 62
}
return result
}
// Generate an 8-character unique ID
func generateID() string {
EPOCH_START := time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC)
now := time.Now().UTC().UnixMilli() - EPOCH_START.UnixMilli()
random := rand.Intn(1000) // 3-digit random number (000-999)
id := now*1000 + int64(random) // Combine timestamp + randomness
return fmt.Sprintf("%08s", toBase62(id))[:8] // Ensure fixed 8 chars
}
func main() {
fmt.Println(generateID()) // Example output: "5F3G8kL2"
}