Top Related Projects
Go package for UUIDs based on RFC 4122 and DCE 1.1: Authentication and Security Services.
UUID package for Go
Universally Unique Lexicographically Sortable Identifier (ULID) in Go
xid is a globally unique id generator thought for the web
K-Sortable Globally Unique IDs
:mushroom: A generator library for concise, unambiguous and URL-safe UUIDs
Quick Overview
gofrs/uuid is a Go package that provides UUID (Universally Unique Identifier) generation and parsing functionality. It's a fork of the original satori/go.uuid library, maintained by the Go community to ensure continued support and improvements.
Pros
- Actively maintained and supported by the community
- Provides both V1 (time-based) and V4 (random) UUID generation
- Offers robust parsing and validation of UUID strings
- Compatible with various UUID formats and representations
Cons
- Limited to UUID versions 1 and 4, lacking support for other versions
- Dependency on external packages for certain functionalities
- May have slightly different API compared to the original satori/go.uuid library
Code Examples
- Generating a new V4 (random) UUID:
package main
import (
"fmt"
"github.com/gofrs/uuid"
)
func main() {
id := uuid.Must(uuid.NewV4())
fmt.Printf("UUID: %s\n", id)
}
- Parsing a UUID string:
package main
import (
"fmt"
"github.com/gofrs/uuid"
)
func main() {
id, err := uuid.FromString("6ba7b810-9dad-11d1-80b4-00c04fd430c8")
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
fmt.Printf("Parsed UUID: %s\n", id)
}
- Generating a V1 (time-based) UUID:
package main
import (
"fmt"
"github.com/gofrs/uuid"
)
func main() {
id, err := uuid.NewV1()
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
fmt.Printf("V1 UUID: %s\n", id)
}
Getting Started
To use gofrs/uuid in your Go project, follow these steps:
-
Install the package:
go get github.com/gofrs/uuid
-
Import the package in your Go code:
import "github.com/gofrs/uuid"
-
Start using the UUID functions in your code, as shown in the examples above.
Competitor Comparisons
Go package for UUIDs based on RFC 4122 and DCE 1.1: Authentication and Security Services.
Pros of google/uuid
- Developed and maintained by Google, potentially offering better long-term support
- Optimized for performance, particularly in high-concurrency scenarios
- Implements RFC 4122 UUIDs with support for version 1, 3, 4, and 5
Cons of google/uuid
- Less flexible API compared to gofrs/uuid
- Fewer utility functions for UUID manipulation and validation
- Limited support for custom UUID formats or non-standard implementations
Code Comparison
gofrs/uuid:
u, err := uuid.NewV4()
if err != nil {
log.Fatalf("failed to generate UUID: %v", err)
}
fmt.Printf("UUID: %s\n", u)
google/uuid:
u := uuid.New()
fmt.Printf("UUID: %s\n", u)
Both libraries provide similar functionality for generating UUIDs, but gofrs/uuid offers more explicit error handling. google/uuid's API is simpler and more concise, which may be preferable in some cases.
gofrs/uuid provides additional utility functions like FromBytes
, FromString
, and Equal
, making it more versatile for UUID manipulation. However, google/uuid's focus on performance and simplicity may be advantageous in high-throughput applications or when basic UUID functionality is sufficient.
UUID package for Go
Pros of go.uuid
- More established and widely used in the Go community
- Supports a broader range of UUID versions (v1, v2, v3, v4, v5)
- Provides additional utility functions for UUID manipulation
Cons of go.uuid
- Less actively maintained, with fewer recent updates
- May have compatibility issues with newer Go versions
- Lacks some performance optimizations found in uuid
Code Comparison
go.uuid:
import "github.com/satori/go.uuid"
id := uuid.NewV4()
uuid:
import "github.com/gofrs/uuid"
id, err := uuid.NewV4()
The main difference in usage is that uuid returns an error along with the UUID, allowing for better error handling.
Summary
While go.uuid has been a popular choice for UUID generation in Go projects, uuid has emerged as a more actively maintained alternative. uuid offers improved performance, better compatibility with recent Go versions, and more robust error handling. However, go.uuid still provides a wider range of UUID versions and utility functions. Developers should consider their specific requirements and the trade-offs between the two libraries when choosing which to use in their projects.
Universally Unique Lexicographically Sortable Identifier (ULID) in Go
Pros of ulid
- Lexicographically sortable, allowing for efficient database indexing
- Includes a timestamp component, useful for time-based sorting
- Shorter string representation (26 characters) compared to UUID (36 characters)
Cons of ulid
- Less widely adopted than UUID, potentially limiting compatibility
- Not a direct drop-in replacement for UUID in all cases
- Requires additional dependencies for some language implementations
Code Comparison
ulid:
id := ulid.MustNew(ulid.Timestamp(time.Now()), entropy)
fmt.Println(id.String()) // 01F8X7AYXZYMK2T4S1SKTWVT8H
uuid:
id := uuid.Must(uuid.NewV4())
fmt.Println(id.String()) // 6ba7b810-9dad-11d1-80b4-00c04fd430c8
Both libraries provide simple ways to generate unique identifiers, but ulid offers additional features like sortability and timestamp inclusion. The choice between them depends on specific project requirements, such as compatibility needs, sorting capabilities, and identifier length preferences. ulid may be more suitable for applications prioritizing time-based ordering, while uuid remains a solid choice for general-purpose unique identification.
xid is a globally unique id generator thought for the web
Pros of xid
- Smaller size: 12 bytes compared to UUID's 16 bytes
- Faster generation: Optimized for high performance
- Sortable: IDs are time-ordered, making them suitable for database indexing
Cons of xid
- Less widely adopted: UUID is a more common standard
- Limited uniqueness: Potential for collisions in extremely high-volume scenarios
- Less compatibility: Some systems expect UUID format
Code Comparison
xid:
id := xid.New()
fmt.Printf("xid: %s\n", id.String())
uuid:
id := uuid.Must(uuid.NewV4())
fmt.Printf("uuid: %s\n", id.String())
Both libraries offer simple, one-line ID generation. xid provides a more compact string representation, while uuid follows the standard UUID format. xid is designed for performance and sortability, making it suitable for high-volume, time-sensitive applications. However, uuid offers broader compatibility and is more widely recognized. The choice between them depends on specific project requirements, such as storage constraints, performance needs, and system compatibility.
K-Sortable Globally Unique IDs
Pros of KSUID
- Time-sortable: KSUIDs include a timestamp component, making them naturally sortable by creation time
- Shorter: KSUIDs are more compact (27 characters) compared to UUIDs (36 characters)
- Higher generation rate: Can generate more unique IDs per second than UUIDs
Cons of KSUID
- Less widely adopted: UUIDs are more universally recognized and supported
- No built-in version information: Unlike UUIDs, KSUIDs don't have version metadata
Code Comparison
KSUID:
id := ksuid.New()
fmt.Printf("KSUID: %s\n", id.String())
UUID:
id := uuid.Must(uuid.NewV4())
fmt.Printf("UUID: %s\n", id.String())
Both libraries offer simple ways to generate unique identifiers, but KSUID provides additional features like time-sorting and more compact representation. UUID, being more widely adopted, may be preferable for systems requiring broader compatibility. The choice between the two depends on specific project requirements, such as the need for time-sortable IDs or compatibility with existing systems using UUIDs.
:mushroom: A generator library for concise, unambiguous and URL-safe UUIDs
Pros of shortuuid
- Generates shorter, more compact UUIDs (22 characters vs 36)
- Provides built-in encoding to various formats (base57, base58)
- Includes functions for generating custom alphabet UUIDs
Cons of shortuuid
- Less widely adopted compared to uuid
- May have slightly lower uniqueness due to shorter length
- Limited compatibility with systems expecting standard UUID format
Code Comparison
shortuuid:
id := shortuuid.New()
fmt.Println(id) // Output: something like "3k4FJvA4Qeb8KgvFB9Ez2L"
uuid:
id, err := uuid.NewV4()
if err != nil {
// handle error
}
fmt.Println(id) // Output: something like "f47ac10b-58cc-4372-a567-0e02b2c3d479"
Both libraries provide simple ways to generate UUIDs, but shortuuid focuses on creating more compact identifiers, while uuid adheres to the standard UUID format. The choice between them depends on specific project requirements, such as storage constraints, system compatibility, and desired identifier length.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
UUID
Package uuid provides a pure Go implementation of Universally Unique Identifiers (UUID) variant as defined in RFC-9562. This package supports both the creation and parsing of UUIDs in different formats.
This package supports the following UUID versions:
- Version 1, based on timestamp and MAC address
- Version 3, based on MD5 hashing of a named value
- Version 4, based on random numbers
- Version 5, based on SHA-1 hashing of a named value
- Version 6, a k-sortable id based on timestamp, and field-compatible with v1
- Version 7, a k-sortable id based on timestamp
Project History
This project was originally forked from the github.com/satori/go.uuid repository after it appeared to be no longer maintained, while exhibiting critical flaws. We have decided to take over this project to ensure it receives regular maintenance for the benefit of the larger Go community.
We'd like to thank Maxim Bublis for his hard work on the original iteration of the package.
License
This source code of this package is released under the MIT License. Please see the LICENSE for the full content of the license.
Recommended Package Version
We recommend using v2.0.0+ of this package, as versions prior to 2.0.0 were created before our fork of the original package and have some known deficiencies.
Requirements
This package requires Go 1.19 or later
Usage
Here is a quick overview of how to use this package. For more detailed documentation, please see the GoDoc Page.
package main
import (
"log"
"github.com/gofrs/uuid/v5"
)
// Create a Version 4 UUID, panicking on error.
// Use this form to initialize package-level variables.
var u1 = uuid.Must(uuid.NewV4())
func main() {
// Create a Version 4 UUID.
u2, err := uuid.NewV4()
if err != nil {
log.Fatalf("failed to generate UUID: %v", err)
}
log.Printf("generated Version 4 UUID %v", u2)
// Parse a UUID from a string.
s := "6ba7b810-9dad-11d1-80b4-00c04fd430c8"
u3, err := uuid.FromString(s)
if err != nil {
log.Fatalf("failed to parse UUID %q: %v", s, err)
}
log.Printf("successfully parsed UUID %v", u3)
}
References
- RFC-9562 (replaces RFC-4122)
- DCE 1.1: Authentication and Security Services
Top Related Projects
Go package for UUIDs based on RFC 4122 and DCE 1.1: Authentication and Security Services.
UUID package for Go
Universally Unique Lexicographically Sortable Identifier (ULID) in Go
xid is a globally unique id generator thought for the web
K-Sortable Globally Unique IDs
:mushroom: A generator library for concise, unambiguous and URL-safe UUIDs
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot