Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/robojones/iid
Small, globally unique IDs implemented in Go
https://github.com/robojones/iid
golang id timestamp unique uuid
Last synced: 23 days ago
JSON representation
Small, globally unique IDs implemented in Go
- Host: GitHub
- URL: https://github.com/robojones/iid
- Owner: robojones
- License: mit
- Created: 2019-07-08T17:05:13.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2019-08-26T15:17:00.000Z (about 5 years ago)
- Last Synced: 2023-07-16T13:01:19.161Z (over 1 year ago)
- Topics: golang, id, timestamp, unique, uuid
- Language: Go
- Homepage:
- Size: 18.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
# iid
Small, globally unique IDs implemented in Go.[![GoDoc](https://godoc.org/github.com/robojones/iid?status.svg)](https://godoc.org/github.com/robojones/iid)
## Features
- Can be sorted by creation time
- Globally unique
- Small size (can be stored in a 64 bit integer or an 11 byte string)
- base64url encoded string format: the ids can be used in URLs## ID Format
| 1 Bit | 32 Bit | 31 Bit |
| -------- | -------------------- | ------------------------------------- |
| Not used | Timestamp in seconds | Cryptographically secure random bytes |The first bit is not used so the ID fits into 64 bit signed integers.
## Usage Example
```go
package mainimport (
"github.com/robojones/iid"
"log"
"reflect"
)func main() {
// Generate new iid
id := iid.New()
log.Printf("buffer format: %#v", id)
// Export as base64url string
str := id.String()
log.Printf("base64 string: %s", str)
// Import the id from the string
parsed, err := iid.FromString(str)
if err != nil {
panic(err)
}
log.Printf("parsed iid from string is identical to the original: %t", reflect.DeepEqual(id, parsed))
// Export as uint64
i := id.Uint64()
log.Printf("integer: %d", i)
// Import the id from the integer
parsed = iid.FromUint64(i)
log.Printf("parsed iid from uint64 is identical to original: %t", reflect.DeepEqual(id, parsed))
}
```