ulidgo

package module
v0.0.0-...-231d0da Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 13, 2022 License: MIT Imports: 9 Imported by: 0

README

ulidgo - ULID written in Go

Implement ULID written in Go for learning.

LICENSE

MIT

Author

matsuyoshi30

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrInvalidULIDLen = errors.New("invalid length of ULID")
View Source
var ErrOverflow = errors.New("overflow random value")

ErrOverflow represents error when overflow random value

Functions

func Factory

func Factory() func() *ULID

Factory returns a function which generates new ULID

func Parse

func Parse(s string) (time.Time, error)

Parse parses ULID string and returns the time value it represents.

Types

type ULID

type ULID struct {
	// contains filtered or unexported fields
}

ULID represents ULID

func New

func New(ts int64) (*ULID, error)

New generates new ULID

Example
package main

import (
	"fmt"
	"time"

	"github.com/matsuyoshi30/ulidgo"
)

var Now = func() time.Time { return time.Date(2022, time.April, 7, 1, 2, 30, 45, time.UTC) }

func main() {
	ulid, _ := ulidgo.New(Now().UnixMilli())
	fmt.Println(ulid)
}
Output:

01G00RPN3GNQDPDEA8MJAJS8SJ
Example (Multi)
package main

import (
	"fmt"
	"time"

	"github.com/matsuyoshi30/ulidgo"
)

func main() {
	nt := time.Date(2022, time.April, 8, 1, 2, 30, 45, time.UTC)
	ulid1, _ := ulidgo.New(nt.UnixMilli())
	ulid2, _ := ulidgo.New(nt.UnixMilli())
	ulid3, _ := ulidgo.New(nt.UnixMilli())
	fmt.Printf("%s\n%s\n%s\n", ulid1, ulid2, ulid3)
}
Output:

01G03B3C3GCZVQY8TYZAEPCGQ5
01G03B3C3GCZVQY8TYZAEPCGQ6
01G03B3C3GCZVQY8TYZAEPCGQ7

func (*ULID) Bytes

func (u *ULID) Bytes() []byte

Bytes returns ULID byte slice

func (*ULID) Compare

func (u *ULID) Compare(target []byte) int

Compare returns an integer comparing two ULID byte slice The result will be 0 if u == target, -1 if u < target and +1 if u > target

func (*ULID) String

func (u *ULID) String() string

String implements fmt.Stringer

func (*ULID) Time

func (u *ULID) Time() time.Time

Time returns UTC converted from ULID unix timestamp value

Example
package main

import (
	"fmt"
	"time"

	"github.com/matsuyoshi30/ulidgo"
)

var Now = func() time.Time { return time.Date(2022, time.April, 7, 1, 2, 30, 45, time.UTC) }

func main() {
	ulid, _ := ulidgo.New(Now().UnixMilli())
	fmt.Println(ulid.Time())
}
Output:

2022-04-07 01:02:30 +0000 UTC

func (*ULID) UnixTime

func (u *ULID) UnixTime() int64

UnixTime returns ULID unix timestamp value

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL