The uniqueids
package provides functionality for generating unique identifiers based on timestamps and sequence numbers. It allows you to generate unique matter numbers and invoice numbers with customizable prefixes.
- Generates unique identifiers using a combination of timestamp and sequence number
- Supports customizable epoch for timestamp calculation
- Provides functions for generating unique matter numbers and invoice numbers
- Allows parsing of generated unique IDs to extract timestamp and sequence number
To use the uniqueids
package in your Go project, you can install it using go get
:
go get github.com/mariosplit/uniqueids
To generate a unique matter number, use the GenerateMatterNumber
function:
matterNumber := uniqueids.GenerateMatterNumber()
To generate a unique invoice number with a custom prefix, use the GenerateInvoiceNumber
function:
invoiceNumber := uniqueids.GenerateInvoiceNumber("INV")
To parse a generated unique ID and extract the timestamp and sequence number, use the ParseUniqueID
function:
utcTime, localTime, sequenceNum, err := uniqueids.ParseUniqueID(uniqueID)
if err != nil {
// Handle the error
} else {
// Use the parsed values
fmt.Printf("UTC Time: %s\nLocal Time: %s\nSequence Number: %d\n", utcTime, localTime, sequenceNum)
}
The ParseUniqueID
function returns the UTC time, local time, sequence number, and any error that occurred during parsing.
By default, the uniqueids
package uses a hard-coded epoch of January 1, 2000, for timestamp calculation. If you want to use a different epoch, you can modify the epoch
variable in the package:
uniqueids.epoch = time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC)
The uniqueids
package is designed to be concurrency-safe. It uses a mutex to synchronize access to the shared state (last timestamp and sequence number) when generating unique IDs.
Here's a complete example of using the uniqueids
package:
package main
import (
"fmt"
"github.com/mariosplit/uniqueids"
)
func main() {
// Generate unique IDs
matterNumber := uniqueids.GenerateMatterNumber()
invoiceNumber := uniqueids.GenerateInvoiceNumber("INV")
fmt.Println("Matter Number:", matterNumber)
fmt.Println("Invoice Number:", invoiceNumber)
// Parse unique IDs
utcTime, localTime, sequenceNum, err := uniqueids.ParseUniqueID(matterNumber)
if err != nil {
fmt.Println("Error parsing Matter Number:", err)
} else {
fmt.Printf("Matter Number - UTC Time: %s\nMatter Number - Local Time: %s\nMatter Number - Sequence Number: %d\n",
utcTime, localTime, sequenceNum)
}
}
The uniqueids
package is open-source software licensed under the MIT License.