Documentation
¶
Overview ¶
Package xuid provides a compact, type-safe identifier system built on UUIDs.
XUID combines the robustness of UUIDs with practical enhancements: - Sortable identifiers using UUIDv7 for chronological ordering - Optional string prefixes for human-readable context (e.g., "user_", "order_") - Base58 encoding for shorter, URL-safe representations - Built-in JSON marshaling and unmarshaling support
Example usage:
// Create a sortable identifier with prefix
userID := xuid.MustNewSortable("user")
fmt.Println(userID.String()) // user_8M7Qq2vR3kGbF9wN5pL2xA
// Parse from string
parsed, err := xuid.Parse("user_8M7Qq2vR3kGbF9wN5pL2xA")
if err != nil {
log.Fatal(err)
}
fmt.Println(parsed.GetPrefix()) // user
Index ¶
- Variables
- func IsEmpty(xid XUID) bool
- func IsValid(idstr string) bool
- type XUID
- func Must(xid XUID, err error) XUID
- func MustNewRandom(prefix string) XUID
- func MustNewSortable(prefix string) XUID
- func MustParse(idstr string) XUID
- func New() (XUID, error)
- func NewRandom(prefix string) (XUID, error)
- func NewSortable(prefix string) (XUID, error)
- func NewWith(id uuid.UUID, prefix string) (XUID, error)
- func NilUUID() (XUID, error)
- func Parse(idstr string) (XUID, error)
- func (x XUID) Equal(y XUID) bool
- func (x XUID) GetPrefix() string
- func (x XUID) GetUUID() uuid.UUID
- func (x XUID) IsRandom() bool
- func (x XUID) IsSortable() bool
- func (x XUID) MarshalJSON() ([]byte, error)
- func (x *XUID) Scan(value interface{}) error
- func (x *XUID) SetPrefix(prefix string) *XUID
- func (x XUID) String() string
- func (x *XUID) UnmarshalJSON(data []byte) error
- func (x XUID) Value() (driver.Value, error)
Constants ¶
This section is empty.
Variables ¶
var ( ErrInvalidUUIDString = errors.New("UUID string is invalid") ErrParse = errors.New("XUID string cannot be parsed") )
Functions ¶
Types ¶
type XUID ¶
type XUID struct {
// contains filtered or unexported fields
}
func MustNewRandom ¶
func MustNewSortable ¶
func NewSortable ¶
func (XUID) IsSortable ¶
func (XUID) MarshalJSON ¶
func (*XUID) Scan ¶
Scan implements the sql.Scanner interface. This allows XUID to be loaded from SQL databases. Note: The prefix information is lost when loading from database. You should reconstruct XUIDs with their appropriate prefixes after loading.
func (*XUID) SetPrefix ¶
SetPrefix sets the prefix field to the specified prefix. This is useful when loading XUIDs from database and need to restore the prefix.