ulid/examples/simple/main.go

35 lines
762 B
Go

package main
import (
"fmt"
"math/rand"
"time"
"git.wisellama.rocks/Wisellama/ulid"
)
func main() {
t := time.Now()
// Warning: Using time.Now() for the entropy seed is bad if you
// have multiple instances all starting up at the same time who
// might write to the same data store.
//
// The default rand.NewSource() is also bad for multiple
// threads. You either need to implement your own locking
// mechanism or use a LockedSource:
// https://pkg.go.dev/golang.org/x/exp/rand#LockedSource
//
// See the other examples for better alternatives.
//
// nolint
entropy := rand.New(rand.NewSource(t.UnixMilli()))
u, err := ulid.NewULIDString(t, entropy)
if err != nil {
fmt.Printf("error: %v\n", err)
}
fmt.Printf("ULID: %s\n", u)
}