Documentation
¶
Overview ¶
Package guid provides fast, efficient, cryptographically secure 128-bit GUID generation and manipulation. It supports Base64Url encoding/decoding, sequential sortable GUIDs (for PostgreSQL and SQL Server), and is optimized for performance. It includes a high-throughput, drop-in replacement for crypto/rand.Reader for generating secure random bytes.
Index ¶
- Constants
- Variables
- func DecodeBase64URL(dst []byte, src []byte) (ok bool)
- func NewString() string
- func Read(b []byte) (n int, err error)
- type Guid
- func (guid *Guid) EncodeBase64URL(dst []byte) error
- func (guid Guid) MarshalBinary() (data []byte, err error)
- func (g Guid) MarshalJSON() ([]byte, error)
- func (guid *Guid) MarshalText() ([]byte, error)
- func (guid *Guid) String() string
- func (guid *Guid) UnmarshalBinary(data []byte) error
- func (g *Guid) UnmarshalJSON(data []byte) error
- func (guid *Guid) UnmarshalText(data []byte) error
- type GuidPG
- type GuidSS
Examples ¶
Constants ¶
const ( GuidByteSize = 16 // Size of a Guid in bytes GuidBase64UrlByteSize = 22 // Base64Url encoding of a Guid is 22 characters )
Variables ¶
var ( // Nil is the nil Guid, with all 128 bits set to zero. Nil Guid = _minGuid // Max is the maximum Guid, with all 128 bits set to one. Max Guid = _maxGuid // Reader is a global, shared instance of a cryptographically secure random number generator. It is safe for concurrent use. Reader reader )
var ( // ErrInvalidBase64UrlGuidEncoding is returned when a Base64Url string does not represent a valid Guid. ErrInvalidBase64UrlGuidEncoding = errors.New("invalid Base64Url Guid encoding (invalid characters, or length != 22)") // ErrInvalidGuidSlice is returned when a byte slice cannot represent a valid Guid (length < 16 bytes). ErrInvalidGuidSlice = errors.New("invalid Guid slice (length < 16 bytes)") // ErrBufferTooSmallBase64Url is returned when a destination slice is too small to receive the text-encoded Guid. ErrBufferTooSmallBase64Url = fmt.Errorf("buffer is too small (length < %d bytes)", GuidBase64UrlByteSize) )
Functions ¶
func DecodeBase64URL ¶ added in v1.0.6
DecodeBase64URL decodes a Base64Url-encoded src byte slice into a Guid dst byte slice. Does not panic on invalid input. dst must be at least 16 bytes long and src must be at least 22 bytes long (returns false otherwise). dst is modified even if the function returns false.
func NewString ¶ added in v1.0.6
func NewString() string
NewString generates a new cryptographically secure Guid, and returns it as a Base64Url string. NewString is equivalent to "g := guid.New(); return g.String();".
Types ¶
type Guid ¶
type Guid [GuidByteSize]byte
Guid is a 16-byte (128-bit) cryptographically random value.
func New ¶
func New() (g Guid)
New generates a new cryptographically secure Guid.
Example ¶
g := New() // new random Guid fmt.Println(&g) // calls g.String(), which returns the Base64Url encoded string
func Parse ¶ added in v1.0.6
Parse parses a Base64Url-encoded string into the Guid. Returns an error if the string is not a valid Guid encoding.
func ParseBytes ¶ added in v1.0.6
ParseBytes parses a Base64Url-encoded string represented as a byte slice into the Guid. Returns an error if the string byte slice is not a valid Guid encoding. ParseBytes is like Parse, except it parses a string byte slice instead of a string.
func (*Guid) EncodeBase64URL ¶ added in v1.0.6
EncodeBase64URL encodes the Guid into the provided dst as Base64Url.
func (Guid) MarshalBinary ¶ added in v1.0.6
MarshalBinary implements the encoding.BinaryMarshaler interface for Guid.
func (Guid) MarshalJSON ¶ added in v1.0.7
MarshalJSON implements the json.Marshaler interface. It marshals the Guid to its Base64Url string representation.
func (*Guid) MarshalText ¶ added in v1.0.6
MarshalText implements encoding.TextMarshaler.
func (*Guid) String ¶ added in v1.0.6
String returns a Base64Url-encoded string representation of the Guid.
Example ¶
// g is a 16-byte Guid represented as a hex string "0123456789abcdef0123456789abcdef" var g Guid = [16]byte{0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x10, 0x32, 0x54, 0x76, 0x98, 0xba, 0xdc, 0xfe} fmt.Println(&g) // calls g.String(), which returns the Base64Url encoded string
Output: ASNFZ4mrze8QMlR2mLrc_g
func (*Guid) UnmarshalBinary ¶ added in v1.0.6
UnmarshalBinary implements the encoding.BinaryUnmarshaler interface for Guid.
func (*Guid) UnmarshalJSON ¶ added in v1.0.7
UnmarshalJSON implements the json.Unmarshaler interface. It unmarshals a JSON string into a Guid.
func (*Guid) UnmarshalText ¶ added in v1.0.6
UnmarshalText implements encoding.TextUnmarshaler.
type GuidPG ¶ added in v1.0.8
type GuidPG struct {
Guid // embedded
}
GuidPG is a 16-byte (128-bit) PostgreSQL sortable Guid formed as [8-byte time.Now() timestamp][8 random bytes] GuidPG is optimized for use as a PostgreSQL index key.
type GuidSS ¶ added in v1.0.8
type GuidSS struct {
Guid // embedded
}
GuidSS is a 16-byte (128-bit) SQL Server sortable Guid formed as [8 random bytes][8 bytes of SQL Server ordered time.Now() timestamp] GuidSS is optimized for use as a SQL Server index or clustered key.