id

package module
v0.0.2-0...-f13774a Latest Latest
Warning

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

Go to latest
Published: Dec 16, 2019 License: MIT Imports: 10 Imported by: 0

README

id

GoDoc

Package id defines a globally unique 128-bit identifier.

Usage Example

Calling id.New() produces a new, unique id.ID:

userID := id.New()
fmt.Printf("%v", userID)
15bffdea7ae1138c6c57ad5fb01ed08a

Structure

An id.ID is actually defined as [16]byte under the hood. Generating a new id.ID takes the current time, with nanosecond precision, and converts this to a uint64. This becomes the first 64 bits of the identifier. The last 64 bits are read from a CSPRNG.

Interfaces

The id.ID type satisfies a number of interfaces:

  • fmt.Stringer - Returns the identifier in string format, e.g. "15bffdea7ae1138c6c57ad5fb01ed08a"

  • json.Marshaler - Marshals to a JSON string, like above.

  • json.Unmarshaler - Marshals from a JSON string.

  • driver.Value - Converts to a string to be stored in SQL databases.

  • sql.Scanner - Parses strings from SQL queries.

Documentation

Index

Constants

View Source
const ByteSize = 16

ByteSize defines the size of the identifiers produced by this package, in bytes.

Variables

View Source
var Empty = ID{}

Empty represents the empty ID (a sequence of 128 0 bits).

Functions

This section is empty.

Types

type ID

type ID [ByteSize]byte

ID represents a unique 128-bit identifier.

func New

func New() ID

New creates a new ID sourced from the current time and a CSPRNG.

func Parse

func Parse(hexID string) (ID, error)

Parse parses the provided hex string, returning an error if parsing fails.

func (ID) IsValid

func (id ID) IsValid() bool

IsValid checks if an ID is valid.

func (ID) MarshalJSON

func (id ID) MarshalJSON() ([]byte, error)

MarshalJSON returns the JSON value of an ID.

func (*ID) Scan

func (id *ID) Scan(src interface{}) error

Scan is used to read out of a database.

func (ID) String

func (id ID) String() string

String returns the string representation of the identifier.

func (*ID) UnmarshalJSON

func (id *ID) UnmarshalJSON(raw []byte) error

UnmarshalJSON returns the parsed ID from a JSON value.

func (ID) Value

func (id ID) Value() (driver.Value, error)

Value is implemented to be database friendly.

Jump to

Keyboard shortcuts

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