Documentation
¶
Overview ¶
Package tuid provides facilities for generating and working with Time-based Unique Identifiers (TUID).
A TUID is an immutable, chronologically sortable, unique identifier with an embedded nanosecond-resolution timestamp. Internally, it is a big integer value, expressed as a case-sensitive base-62 string (digits 0-9, characters A-Z, and characters a-z). The left bits are the embedded nanoseconds since epoch, and the right 32 bits are entropy (a random integer). Valid IDs in the 21st century are 16 characters long, and the bit length ranges from 92-94 bits.
Example: `91Mq07yx9IxHCi5Y` is a TUID with a UTC timestamp of `2021-03-08T05:54:09.208207Z`. Because the left bits are the nanoseconds since epoch, the IDs sort in chronological order.
Index ¶
Examples ¶
Constants ¶
const MaxID = "MuklDY5bgW1s9Ev2"
MaxID is the first ID at 2100-01-01T00:00:00Z
const MinID = "5Hr02eJHAfTt1tTM"
MinID is the first ID at 2000-01-01T00:00:00Z
Variables ¶
This section is empty.
Functions ¶
func Duration ¶
Duration returns the number of nanoseconds between two TUIDs as a time.Duration
Example ¶
package main
import (
"fmt"
"github.com/voxtechnica/tuid-go"
)
func main() {
startID := tuid.TUID("9AxgffrWr9qCnfIT")
stopID := tuid.TUID("9AxjEL0lPtoGAbLE")
// Calculate duration between two TUIDs
duration, _ := tuid.Duration(startID, stopID)
fmt.Println(duration)
}
Output: 32m2.322640879s
func IsValid ¶
IsValid checks to see if the provided TUID has valid characters and a reasonable embedded timestamp
Example ¶
package main
import (
"fmt"
"github.com/voxtechnica/tuid-go"
)
func main() {
validID := tuid.TUID("91Mq07yx9IxHCi5Y")
invalidID := tuid.TUID("not-a-valid-tuid")
fmt.Println(tuid.IsValid(validID))
fmt.Println(tuid.IsValid(invalidID))
}
Output: true false
Types ¶
type TUID ¶
type TUID string
TUID is a Time-based Unique Identifier (e.g. 91Mq07yx9IxHCi5Y) that has an embedded timestamp and sorts chronologically. It's a 16-digit base-62 big integer, where the leftmost bits are a timestamp with nanosecond resolution (e.g. 2021-03-08T05:54:09.208207000Z) and the rightmost 32 bits are "entropy" (a random number), providing some assurance of uniqueness if multiple IDs are created at the same moment. Collisions in a single information system are extremely unlikely. The Zero value of a TUID is an empty string.
func FirstIDWithTime ¶
FirstIDWithTime creates a TUID with the provided timestamp and zero entropy, useful for query offsets
Example ¶
package main
import (
"fmt"
"time"
"github.com/voxtechnica/tuid-go"
)
func main() {
// Create a specific time for our example
t := time.Date(2021, 3, 8, 5, 54, 9, 208207000, time.UTC)
// Create the first TUID for this exact nanosecond (zero entropy)
id := tuid.FirstIDWithTime(t)
fmt.Println(id)
}
Output: 91Mq07yx9IvZeYJE
func NewID ¶
func NewID() TUID
NewID creates a new TUID with the current UTC time.
Example ¶
package main
import (
"fmt"
"github.com/voxtechnica/tuid-go"
)
func main() {
id := tuid.NewID()
// A TUID is always 16 characters long
fmt.Println(len(id))
}
Output: 16
func NewIDWithTime ¶
NewIDWithTime creates a TUID with the provided timestamp
func NewIDWithTimeAndEntropy ¶ added in v1.0.2
NewIDWithTimeAndEntropy creates a TUID with the provided timestamp and entropy be very careful with this function as you get ID collisions if the caller does not manage entropy values carefully
func (TUID) Entropy ¶
Entropy extracts the random 32 bits from the specified TUID
Example ¶
package main
import (
"fmt"
"github.com/voxtechnica/tuid-go"
)
func main() {
id := tuid.TUID("9AxhASm3k3MVH8se")
// Extract the entropy (random bits)
entropy, _ := id.Entropy()
fmt.Println(entropy)
}
Output: 1588339652
func (TUID) Info ¶
Info extracts the timestamp and entropy from the specified TUID. The returned timestamp is in UTC.
func (TUID) Time ¶
Time extracts the embedded timestamp from the specified TUID. The returned time is in UTC.
Example ¶
package main
import (
"fmt"
"time"
"github.com/voxtechnica/tuid-go"
)
func main() {
id := tuid.TUID("91Mq07yx9IxHCi5Y")
// Extract the embedded timestamp
ts, _ := id.Time()
// Format the timestamp
fmt.Println(ts.Format(time.RFC3339Nano))
}
Output: 2021-03-08T05:54:09.208207Z