Skip to content

Instantly share code, notes, and snippets.

@youknowcast
Last active April 22, 2021 05:30
Show Gist options
  • Save youknowcast/8d5ca214998314f287f0fbfd0f8d736f to your computer and use it in GitHub Desktop.
Save youknowcast/8d5ca214998314f287f0fbfd0f8d736f to your computer and use it in GitHub Desktop.
% go run ulid/ulid.go
Unixtime with nanosec
01F3W0VG46FH5P40MEEY68ME80
01F3W0VG467PHG5QEV57V2K7PF
01F3W0VG46GSWCC2GEC8G0N3WW
01F3W0VG46PX26DX5B59Q2GPX3
Unixtime with sec
01F3W0VG0GAH38AX7EJ2NJ557A
01F3W0VG0GAH38AX7EJ2NJ557A
01F3W0VG0GAH38AX7EJ2NJ557A
01F3W0VG0GAH38AX7EJ2NJ557A
...but dont create entropy(io.Reader) everytime.
01F3W0VG0GAH38AX7EJ2NJ557A
01F3W0VG0GAH38AX7EJ5Y176FG
01F3W0VG0GAH38AX7EJ7AKEDC3
01F3W0VG0GAH38AX7EJ7W4GKJS
package main
import (
"fmt"
"math/rand"
"time"
"github.com/oklog/ulid"
)
func main() {
{
gen1 := func() string {
now := time.Now()
t := now.UnixNano()
entropy := ulid.Monotonic(rand.New(rand.NewSource(t)), 0)
id := ulid.MustNew(ulid.Timestamp(now), entropy).String()
return id
}
gen2 := func() string {
t := time.Unix(time.Now().Unix(), 0)
entropy := ulid.Monotonic(rand.New(rand.NewSource(t.UnixNano())), 0)
id := ulid.MustNew(ulid.Timestamp(t), entropy).String()
return id
}
var gen3 func() string
{
t := time.Unix(time.Now().Unix(), 0)
entropy := ulid.Monotonic(rand.New(rand.NewSource(t.UnixNano())), 0)
gen3 = func() string {
id := ulid.MustNew(ulid.Timestamp(t), entropy).String()
return id
}
}
fmt.Println("Unixtime with nanosec")
fmt.Println(gen1())
fmt.Println(gen1())
fmt.Println(gen1())
fmt.Println(gen1())
fmt.Println("Unixtime with sec")
fmt.Println(gen2())
fmt.Println(gen2())
fmt.Println(gen2())
fmt.Println(gen2())
fmt.Println("...but dont create entropy(io.Reader) everytime.")
fmt.Println(gen3())
fmt.Println(gen3())
fmt.Println(gen3())
fmt.Println(gen3())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment