uuid

package module
v0.0.0-...-414c390 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2023 License: MIT Imports: 12 Imported by: 2

README

UUID - RFC4122

Build Status Code Coverage Go Report Card Release Version GoDoc License


Another UUID generator and parser for Go. Returns UUID or Nil

Usage

go get -u github.com/jquiterio/uuid

func main(){

  // new V4
  u := uuid.New() // or u := uuid.NewV4() generates a new UUID v4.
  u.String() // returns a string uuid
  u.Bytes() // retruns a byte slice

  u5 := uuid.NewV5() // generates a new UUID v5
  u5.String()
  u5.Bytes() // retruns a byte slice

  // Parse UUID
  // Get UUID from String
  ufs := uuid.Parse("c5302009-7ff6-47d2-9a1c-72601da3e3e5")
  ufs.String()
  // Get UUID from Bytes
  ufb := Parse(uuid.New().Bytes())

  // IsValid: Check if UUID is Valid
  uuid.IsValid("something") // may return false
  uuid.IsValid("c5302009-7ff6-47d2-9a1c-72601da3e3e5") // take ufmay return true
}

Go struct with GORM

type User struct {
  UUID uuid.UUID `gorm:"type:uuid" json:"uuid"`
  Name string `json:"name"`
}

Documentation

Index

Constants

View Source
const (
	// V4 Represents the UUID version 4
	V4 byte = 0x04
	// V5 Represents the UUID version 5
	V5 byte = 0x05
)
View Source
const (

	// VariantNCS Represents the UUID variant NCS
	VariantNCS byte
	// VariantRFC4122 Represents the UUID variant RFC4122
	VariantRFC4122
	// VariantMicrosoft Represents the UUID variant Microsoft
	VariantMicrosoft
	// VariantFuture Represents the UUID variant Future
	VariantFuture
)

Variables

View Source
var Nil = UUID{}

Nil is the Nil UUID.

Functions

func Equal

func Equal(u1, u2 UUID) bool

func IsValid

func IsValid(in interface{}) bool

func NewString

func NewString() string

Types

type NullUUID

type NullUUID struct {
	UUID  UUID `json:"uuid"`
	Valid bool `json:"valid"`
}

NullUUID is a nullable UUID.

func (NullUUID) MarshalJSON

func (nu NullUUID) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface.

func (*NullUUID) Scan

func (nu *NullUUID) Scan(src interface{}) error

Scan implements the sql.Scanner interface.

func (*NullUUID) UnmarshalJSON

func (nu *NullUUID) UnmarshalJSON(b []byte) error

UnmarshalJSON implements the json.Unmarshaler interface.

func (NullUUID) Value

func (nu NullUUID) Value() (driver.Value, error)

Value implements the driver.Valuer interface.

type UUID

type UUID [16]byte

UUID is a 128 bit (16 byte) Universal Unique IDentifier.

var (
	// NSDNS is the IETF namespace for the DNS.
	NSDNS UUID = Parse("6ba7b810-9dad-11d1-80b4-00c04fd430c8")
	// NSURL is the IETF namespace for the URL.
	NSURL UUID = Parse("6ba7b811-9dad-11d1-80b4-00c04fd430c8")
	// NSOID is the IETF namespace for the OID.
	NSOID UUID = Parse("6ba7b812-9dad-11d1-80b4-00c04fd430c8")
	// NSX500 is the IETF namespace for the X500.
	NSX500 UUID = Parse("6ba7b814-9dad-11d1-80b4-00c04fd430c8")
)

func New

func New() UUID

New returns a new UUID based on the string input. It returns Nil if the input is not a valid UUID.

func NewV4

func NewV4() UUID

NewV4 returns a new random UUID. It returns Nil if the input is not a valid UUID.

func NewV5

func NewV5(ns UUID, name string) UUID

NewV5 returns a new UUID based on the SHA-1 hash of the namespace UUID and name. It returns Nil if the input is not a valid UUID.

func Parse

func Parse(in interface{}) UUID

Parse decodes s string into a UUID or returns NilUUID if the string is invalid. formats: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx urn:uuid:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx} xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.

func (UUID) Bytes

func (u UUID) Bytes() []byte

Bytes returns the raw UUID bytes.

func (*UUID) Equal

func (u *UUID) Equal(u2 UUID) bool

func (UUID) MarshalJSON

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

MarshalJSON implements the json.Marshaler interface.

func (*UUID) Scan

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

Scan implements the sql.Scanner interface.

func (UUID) String

func (u UUID) String() string

String returns the UUID in it's canonical form, a 32

func (UUID) ToBytes

func (u UUID) ToBytes() []byte

ToBytes returns the UUID as a byte slice.

func (*UUID) UnmarshalBinary

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

UnmarshalBinary implements the encoding.BinaryUnmarshaler interface.

func (*UUID) UnmarshalJSON

func (u *UUID) UnmarshalJSON(b []byte) error

UnmarshalJSON implements the json.Unmarshaler interface.

func (*UUID) UnmarshalText

func (u *UUID) UnmarshalText(text string) error

UnmarshalText implements the encoding.TextUnmarshaler interface.

func (UUID) Value

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

Value implements the driver.Valuer interface.

func (UUID) Variant

func (u UUID) Variant() byte

Variant returns the variant of the UUID

func (UUID) Version

func (u UUID) Version() byte

Version returns the version of the UUID

Jump to

Keyboard shortcuts

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