idgen

package module
v0.0.0-...-c7fc62c Latest Latest
Warning

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

Go to latest
Published: May 29, 2019 License: MIT Imports: 8 Imported by: 2

README

idgen

Go Report Card Build Status Coverage

Generates url safe lexically sorted universally unique ids with a prefix in go. Inspired from Stripe.

Usage

package main

import (
    "fmt"

    "github.com/appointy/idgen"
)

func main()  {
    id := idgen.New("cus")
    fmt.Println(id)
    // Output: cus_0000XSNJG0MQJHBF4QX1EFD6Y3
}
Depends on

oklog/ulid

Documentation

Overview

Package idgen generates url safe lexically sorted universally unique ids with a prefix in go. Inspired from Stripe.

Index

Constants

View Source
const (
	// SeparatorByte is the byte representation of the separator '_' character.
	SeparatorByte = byte('_')

	// SeparatorString is the string representation of the separator '_' character.
	SeparatorString = string(SeparatorByte)
)

Variables

View Source
var (
	// ErrMalformed represents and error that is returned in case of malformed ulids
	ErrMalformed = errors.New("idgen: malformed ulid")
)

Functions

func New

func New(prefix string) string

New generates a lexically sorted, url safe Id with a prefix. Eg: cus_JSfjkdjf333j46, i.e. {prefix}_{ulid}

func Prefix

func Prefix(id string) string

Prefix returns the prefix of the id. Id should be of type {prefix}{Separator}* If id is malformed it returns an empty string.

func Time

func Time(id string) (uint64, error)

Time returns the Unix time in milliseconds encoded in the id. id should be of the form {prefix}{Separator}{ulid}. The prefix and separator are optional. ErrMalformed is returned in case of a malformed id.

Types

type Generator

type Generator struct {
	Prefix string
}

Generator generates ids with the provided prefix

func (Generator) New

func (p Generator) New() string

New generates a lexically sorted, url safe Id with available prefix

Jump to

Keyboard shortcuts

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