uuid

package module
v0.0.0-...-892ae55 Latest Latest
Warning

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

Go to latest
Published: Aug 31, 2018 License: MIT Imports: 8 Imported by: 1

README

Unique IDs in RFC 4122

GoDoc Build Status Coverage Status

Package uuid contains functions for creating and working with unique IDs in RFC 4122.

The main difference from other similar packages:

  1. support only versions of UUID V4;
  2. full support for serialization/deserialization to text and binary form, including JSON, BSON, XML and databases.
package main

import (
	"encoding/json"
	"log"

	"github.com/mdigger/uuid"
	"gopkg.in/mgo.v2/bson"
)

func main() {
	uuidData := uuid.New()
	println("UUID:   ", uuidData.String())
	data, err := json.Marshal(uuidData)
	if err != nil {
		log.Fatal(err)
	}
	println("JSON:  ", string(data))
	var newUUID uuid.UUID
	if err := json.Unmarshal(data, &newUUID); err != nil {
		log.Fatal(err)
	}
	println("RESTORE:", newUUID.String())
	data, err = bson.Marshal(uuidData)
	if err != nil {
		log.Fatal(err)
	}
	if err := bson.Unmarshal(data, &newUUID); err != nil {
		log.Fatal(err)
	}
	println("RESTORE:", newUUID.String())
}

Documentation

Overview

Package uuid contains functions for creating and working with unique IDs in RFC 4122.

The main difference from other similar packages:

1. support only versions of UUID V4

2. full support for serialization/deserialization to text and binary form, including JSON, BSON, XML and databases.

Example
package main

import (
	"encoding/json"
	"log"

	"github.com/globalsign/mgo/bson"
	"github.com/mdigger/uuid"
)

func main() {
	uuidData := uuid.New()
	println("UUID:   ", uuidData.String())
	data, err := json.Marshal(uuidData)
	if err != nil {
		log.Fatal(err)
	}
	println("JSON:  ", string(data))
	var newUUID uuid.UUID
	if err := json.Unmarshal(data, &newUUID); err != nil {
		log.Fatal(err)
	}
	println("RESTORE:", newUUID.String())
	data, err = bson.Marshal(uuidData)
	if err != nil {
		log.Fatal(err)
	}
	if err := bson.Unmarshal(data, &newUUID); err != nil {
		log.Fatal(err)
	}
	println("RESTORE:", newUUID.String())
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type UUID

type UUID [16]byte

UUID describes the format of the unique identifier corresponding to RFC 4122.

func New

func New() (uuid UUID)

New returns a new random unique identifier.

func Parse

func Parse(s string) (uuid UUID, err error)

Parse parses and returns a UUID from its string representation.

func (UUID) Bytes

func (u UUID) Bytes() []byte

Bytes returns a byte representation of the UUID.

func (UUID) Equal

func (u UUID) Equal(uuid UUID) bool

Equal returns true if the UUID is equal to the current compare.

func (UUID) GetBSON

func (u UUID) GetBSON() (interface{}, error)

GetBSON returns a representation of the unique identifier in the form of the BSON binary object with the set type UUID.

func (UUID) MarshalBinary

func (u UUID) MarshalBinary() (data []byte, err error)

MarshalBinary provides the HMDI supports the interface encoding.BinaryMarshaler.

func (UUID) MarshalText

func (u UUID) MarshalText() ([]byte, error)

MarshalText provides the HMDI supports the interface encoding.TextMarshaler. The result of the encoding corresponds exactly to the canonical string representation.

func (*UUID) Scan

func (u *UUID) Scan(src interface{}) error

Scan provides support for the sql interface.Scanner. For the 16 byte sequence is used UnmarshalBinary, whereas the longer sequence, or string is used UnmarshalText.

func (*UUID) SetBSON

func (u *UUID) SetBSON(raw bson.Raw) error

SetBSON deserializes the UUID from the internal binary representation of JSON.

func (UUID) String

func (u UUID) String() string

String returns the canonical string representation of a UUID:

xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.

func (*UUID) UnmarshalBinary

func (u *UUID) UnmarshalBinary(data []byte) error

UnmarshalBinary provides support for the interface encoding.BinaryUnmarshaler. Returns an error if data size is not equal to 16 bytes.

func (*UUID) UnmarshalText

func (u *UUID) UnmarshalText(text []byte) (err error)

UnmarshalText provides support for the interface encoding.TextUnmarshaler. The following formats are supported:

"6ba7b810-9dad-11d1-80b4-00c04fd430c8",
"{6ba7b810-9dad-11d1-80b4-00c04fd430c8}",
"urn:uuid:6ba7b810-9dad-11d1-80b4-00c04fd430c8"

func (UUID) Value

func (u UUID) Value() (driver.Value, error)

Value provides support for the interface driver.Valuer.

func (UUID) Version

func (u UUID) Version() uint

Version returns the version of the algorithm used to generate the UUID.

Jump to

Keyboard shortcuts

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