https://github.com/cmackenzie1/go-uuid
A simple, stdlib only, go module for generating RFC9562 UUIDs (Universally Unique IDentifiers).
https://github.com/cmackenzie1/go-uuid
cli rfc4122 rfc9562 uuid uuidv4 uuidv7
Last synced: 4 months ago
JSON representation
A simple, stdlib only, go module for generating RFC9562 UUIDs (Universally Unique IDentifiers).
- Host: GitHub
- URL: https://github.com/cmackenzie1/go-uuid
- Owner: cmackenzie1
- License: mit
- Created: 2023-01-24T02:45:10.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2025-01-03T19:29:14.000Z (over 1 year ago)
- Last Synced: 2025-04-05T15:17:33.677Z (about 1 year ago)
- Topics: cli, rfc4122, rfc9562, uuid, uuidv4, uuidv7
- Language: Go
- Homepage: https://pkg.go.dev/github.com/cmackenzie1/go-uuid
- Size: 22.5 KB
- Stars: 16
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# go-uuid
[](https://pkg.go.dev/github.com/cmackenzie1/go-uuid)

A simple, stdlib only, go module for generating version 4 (random) and version 7 (time-based) UUIDs (**U**niversally **U**nique **ID**entifiers). This library is based on the [RFC9562](https://www.rfc-editor.org/rfc/rfc9562.html) specification.
## Installation
### CLI
```bash
# Using go install
go install github.com/cmackenzie1/go-uuid/cmd/uuid@latest
```
Or download the binary from [Releases](https://github.com/cmackenzie1/go-uuid/releases)
### Package
```bash
go get github.com/cmackenzie1/go-uuid
```
## Supported versions
| Version | Variant | Details |
| ----------- | ------- | ----------------------------------------------------------------------------------- |
| `Version 4` | `10` | Pure random as defined in [RFC9562](https://www.rfc-editor.org/rfc/rfc9562.html). |
| `Version 7` | `10` | Time-sortable as defined in [RFC9562](https://www.rfc-editor.org/rfc/rfc9562.html). |
## Usage
```go
// example/example.go
package main
import (
"fmt"
"github.com/cmackenzie1/go-uuid"
)
func main() {
v4, err := uuid.NewV4()
if err != nil {
panic(err)
}
fmt.Printf("UUIDv4: %s\n", v4) // c07526de-40e5-418f-93d1-73ba20d2ac2c
v7, _ := uuid.NewV7()
if err != nil {
panic(err)
}
fmt.Printf("UUIDv7: %s\n", v7) // 0185e1af-a3c1-704f-80f5-6fd2f8387f09
}
```
## FAQ
### What are the benefits of this library over X?
- A single library with no external dependencies for multiple types of UUIDs.
- `UUID` type is defined as a fixed-size, `[16]byte`, array which can be used as a map key (instead of the 36 byte
string representation). Over 2x space savings for memory!
- Limited API. As per RFC9562, UUIDs (while containing embedded information), should be treated as opaque
values. There is no temptation to build dependencies on the embedded information if you can't easily access it. 😉
### When should I use UUIDv7 over UUIDv4?
> Non-time-ordered UUID versions such as UUIDv4 have poor database index locality. This means that new
> values created in succession are not close to each other in the index and thus require inserts to be performed at
> random locations. The negative performance effects of which on common structures used for this (B-tree and its
> variants) can be dramatic. [1]
**tl;dr**: if you intend to use the UUID as a database key, use UUIDv7. If you require
purely random IDs, use UUIDv4.
## Contributing
Pull requests are welcome. For major changes, please open an issue first
to discuss what you would like to change.
Please make sure to update tests as appropriate.
## License
[MIT](./LICENSE.md)
[1]: https://www.rfc-editor.org/rfc/rfc9562.html#section-2.1