uuid

package module
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Mar 2, 2019 License: MIT Imports: 8 Imported by: 40

README

Pure Go UUID implementation

license

Version 1.0.0

This is a copy of github.com/nu7hatch/gouuid with some modifications to improve performance. All credit for the original should go to the original author.

This package provides immutable UUID structs and the functions NewV3, NewV4, NewV5 and Parse() for generating versions 3, 4 and 5 UUIDs as specified in RFC 4122.

Installation

Use the go tool:

$ go get github.com/pschlump/uuid

Usage

See documentation and examples for more information.

License

MIT see LICENSE file.

Documentation

Index

Examples

Constants

View Source
const (
	ReservedNCS       byte = 0x80
	ReservedRFC4122   byte = 0x40
	ReservedMicrosoft byte = 0x20
	ReservedFuture    byte = 0x00
)

The UUID reserved variants.

Variables

View Source
var (
	NamespaceDNS, _  = ParseHex("6ba7b810-9dad-11d1-80b4-00c04fd430c8")
	NamespaceURL, _  = ParseHex("6ba7b811-9dad-11d1-80b4-00c04fd430c8")
	NamespaceOID, _  = ParseHex("6ba7b812-9dad-11d1-80b4-00c04fd430c8")
	NamespaceX500, _ = ParseHex("6ba7b814-9dad-11d1-80b4-00c04fd430c8")
)

The following standard UUIDs are for use with NewV3() or NewV5().

Functions

func IsUUID

func IsUUID(s string) bool

Types

type UUID

type UUID [16]byte

A UUID representation compliant with specification in RFC 4122 document.

func NewV3

func NewV3(ns *UUID, name []byte) (u *UUID, err error)

NewV3 will generate a UUID based on the MD5 hash of a namespace identifier and a name.

func NewV4

func NewV4() (u *UUID, err error)

NewV4 will generate a random UUID in the version 4 format.

Example
package main

import (
	"fmt"

	"github.com/pschlump/uuid"
)

func main() {
	u4, err := uuid.NewV4()
	if err != nil {
		fmt.Println("error:", err)
		return
	}
	fmt.Printf("%s\n", u4)
}
Output:

func NewV5

func NewV5(ns *UUID, name []byte) (u *UUID, err error)

NewV5 will generate a UUID based on the SHA-1 hash of a namespace identifier and a name.

Example
package main

import (
	"fmt"

	"github.com/pschlump/uuid"
)

func main() {
	u5, err := uuid.NewV5(uuid.NamespaceURL, []byte("nu7hat.ch"))
	if err != nil {
		fmt.Printf("Error: %s\n", err)
		return
	}
	fmt.Printf("%s\n", u5)
}
Output:

func Parse

func Parse(b []byte) (u *UUID, err error)

Parse creates a UUID object from given bytes slice.

func ParseHex

func ParseHex(s string) (u *UUID, err error)

ParseHex creates a UUID object from given hex string representation. Function accepts UUID string in following formats:

uuid.ParseHex("6ba7b814-9dad-11d1-80b4-00c04fd430c8")
uuid.ParseHex("{6ba7b814-9dad-11d1-80b4-00c04fd430c8}")
uuid.ParseHex("urn:uuid:6ba7b814-9dad-11d1-80b4-00c04fd430c8")
Example
package main

import (
	"fmt"

	"github.com/pschlump/uuid"
)

func main() {
	u, err := uuid.ParseHex("6ba7b810-9dad-11d1-80b4-00c04fd430c8")
	if err != nil {
		fmt.Printf("Error: %s\n", err)
		return
	}
	fmt.Printf("%s\n", u)
}
Output:

func (*UUID) String

func (u *UUID) String() string

Returns unparsed version of the generated UUID sequence.

func (*UUID) Variant

func (u *UUID) Variant() byte

Variant returns the UUID Variant, which determines the internal layout of the UUID. This will be one of the constants: RESERVED_NCS, RFC_4122, RESERVED_MICROSOFT, RESERVED_FUTURE.

func (*UUID) Version

func (u *UUID) Version() uint

Version returns a version number of the algorithm used to generate the UUID sequence.

Jump to

Keyboard shortcuts

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