buid/buid.go (view raw)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
package buid import ( "math/rand" "time" ) const ( base32Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567" buidLength = 18 randomCharsLength = 11 ) func GenerateBUID() string { // Pre-allocate byte slice for result result := make([]byte, buidLength) // Convert the current Unix timestamp to base32 now := time.Now().UTC().Unix() pos := buidLength - randomCharsLength for now > 0 { pos-- result[pos] = base32Alphabet[now&31] now /= 32 } // Generate the random part directly into the result slice for i := buidLength - randomCharsLength; i < buidLength; i++ { // n, _ := rand.Int(rand.Reader, big.NewInt(32)) // Ignoring the error as before // result[i] = base32Alphabet[n.Int64()] result[i] = base32Alphabet[rand.Intn(32)] } return string(result[pos:]) } |