Advertisement
cydside

Ulid generation

Oct 3rd, 2024
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 0.56 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "fmt"
  5.     "log"
  6.     "math/rand"
  7.     "time"
  8.  
  9.     "github.com/oklog/ulid"
  10. )
  11.  
  12. func main() {
  13.     t := time.Now()
  14.     // t := time.Unix(1000000, 0)
  15.     entropy := ulid.Monotonic(rand.New(rand.NewSource(t.UnixNano())), 0)
  16.     // entropy := rand.Reader
  17.     // entropy := ulid.Monotonic(rand.Reader, 0)
  18.     ids := make([]ulid.ULID, 10000)
  19.     for i := range ids {
  20.         ids[i] = ulid.MustNew(ulid.Timestamp(t), entropy)
  21.     }
  22.     seen := make(map[ulid.ULID]bool)
  23.     for _, id := range ids {
  24.         fmt.Println(id)
  25.         if seen[id] {
  26.             log.Fatal("dup")
  27.         }
  28.         seen[id] = true
  29.     }
  30. }
Tags: golang ULID
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement