This code defines a Go package called `randid` for generating...
This code defines a Go package called randid
for generating and working with 128-bit random IDs, along with a base64-like encoding mechanism for their string representation.
Detailed Breakdown:
-
ID Definition (
type ID [2]uint64
):ID
is defined as a 128-bit random value, represented using an array of twouint64
values ([2]uint64
).
-
Constants:
Size
: Represents the length in bytes of anID
(128 bits = 16 bytes).StringLen
: Represents the length of the base64-encoded string representation of anID
(22 characters without padding).
-
Converting ID to Bytes (
func (id ID) Bytes() [Size]byte
):- Converts the 128-bit
ID
into an array of 16 bytes. - The function extracts each byte from the two
uint64
values inID
by applying bit-shifting and masking.
- Converts the 128-bit
-
Converting ID to Base64-like String (
func (id ID) String() string
):- Converts the
ID
into a string representation using a custom unpadded base64-like encoding. - The function uses the
encodeUnrolled
method to encode theID
into 22 characters using a manually optimized approach. - It uses the
unsafe
package to efficiently convert the encoded byte array into a string.
- Converts the
-
Generating New IDs (
func New() ID
):- Generates a new 128-bit random
ID
using two calls torand.Uint64()
to populate the twouint64
values in theID
.
- Generates a new 128-bit random
-
Custom Base64-like Encoding (
func encodeUnrolled(dst *[StringLen]byte, src ID)
):- Encodes the
ID
into a fixed-length custom base64-like string without padding characters (=
). - Utilizes a map of 64 characters (
encodeMap
) to represent 6-bit chunks of the inputID
(base64 encoding works with 6-bit blocks). - The implementation is manually unrolled for performance optimization, processing chunks of 3 bytes from the
ID
at a time and converting them into 4 encoded characters.
- Encodes the
-
Custom Encoding Map (
var encodeMap
):- Defines the 64-character map used for encoding. It includes uppercase letters (
A-Z
), lowercase letters (a-z
), digits (0-9
), and two special characters (-
and_
)—a "URL-safe" base64-like encoding.
- Defines the 64-character map used for encoding. It includes uppercase letters (
Key Points:
- The main functionality of this package is to generate random 128-bit identifiers (
ID
), convert them to a byte representation (Bytes()
), and encode them into a compact and URL-safe string representation (String()
). - The encoding implemented here is a custom take on base64 encoding, optimized for fixed-size input (16 bytes) and omitting the need for padding.
- The code relies on the Go
math/rand
package (specificallyrand.Uint64()
) for generating the random numbers used in theID
.
Example Usage:
To generate and work with an ID
:
package main
import (
"fmt"
"randid"
)
func main() {
// Generate a new random ID
id := randid.New()
// Get the byte representation
bytes := id.Bytes()
fmt.Printf("Bytes: %x\n", bytes)
// Get the string representation
str := id.String()
fmt.Printf("String: %s\n", str)
}
This program would:
- Generate a random 128-bit
ID
. - Print the
ID
as a 16-byte hexadecimal array. - Print the URL-safe string representation (22 characters).
Summary:
This package provides a streamlined and compact way to generate, encode, and represent 128-bit random identifiers, useful for purposes like unique session tokens, object IDs, or other cases requiring collision-resistant random identifiers. The encoding used is a tailored and efficient variation of base64.