UUID package for Go

UUID package for Go language

Build Status Coverage Status GoDoc

This package provides pure Go implementation of Universally Unique Identifier (UUID). Supported both creation and parsing of UUIDs.

With 100% test coverage and benchmarks out of box.

Supported versions:

  • Version 1, based on timestamp and MAC address (RFC 4122)
  • Version 2, based on timestamp, MAC address and POSIX UID/GID (DCE 1.1)
  • Version 3, based on MD5 hashing (RFC 4122)
  • Version 4, based on random numbers (RFC 4122)
  • Version 5, based on SHA-1 hashing (RFC 4122)

Installation

Use the go command:

$ go get github.com/satori/go.uuid

Requirements

UUID package tested against Go >= 1.6.

Example

package main

import (
	"fmt"
	"github.com/satori/go.uuid"
)

func main() {
	// Creating UUID Version 4
	// panic on error
	u1 := uuid.Must(uuid.NewV4())
	fmt.Printf("UUIDv4: %s\n", u1)

	// or error handling
	u2, err := uuid.NewV4()
	if err != nil {
		fmt.Printf("Something went wrong: %s", err)
		return
	}
	fmt.Printf("UUIDv4: %s\n", u2)

	// Parsing UUID from string input
	u2, err := uuid.FromString("6ba7b810-9dad-11d1-80b4-00c04fd430c8")
	if err != nil {
		fmt.Printf("Something went wrong: %s", err)
		return
	}
	fmt.Printf("Successfully parsed: %s", u2)
}

Documentation

Documentation is hosted at GoDoc project.

Links

Copyright

Copyright (C) 2013-2018 by Maxim Bublis [email protected].

UUID package released under MIT License. See LICENSE for details.

Comments
  • NewV4: non-random uuid

    NewV4: non-random uuid

    I'm running this on my macbook pro.

    I'm using uuid.Must(uuid.NewV4()).String() to generate random identifiers. I'm generating theses identifiers at a very low rate of a few dozen per days

    Here are some non-random UUID that I got in the last weeks:

    02524e6f-65a7-4cf5-8000-0000000000002
    6e3ef1c8-0000-4000-8000-0000000000001
    fa07f1e9-a8a0-427f-8103-e34e000000000
    2cfa392b-71d5-4000-8000-000000000000
    5f16db16-56ce-4000-8000-000000000000
    bac73b37-aab3-498b-8000-000000000000
    da5bd82d-4f98-4b51-8000-000000000000
    eb245e52-535f-4274-8350-3a7200000000
    f7510000-0000-4000-8000-000000000000
    5936f955-286e-47ac-8000-000000000000
    0a14da92-0000-4000-8000-000000000000
    80730000-0000-4000-8000-000000000000
    cd2b88a5-0000-4000-8000-000000000000
    

    I think I shouldn't have theses.

  • Implement sql.Scanner and driver.Valuer interfaces

    Implement sql.Scanner and driver.Valuer interfaces

    To support mapping a struct to db with e.g. https://github.com/jmoiron/modl. But since they are interfaces they are not specific to that implementation.

  • not enough arguments in call to uuid.Must

    not enough arguments in call to uuid.Must

    not enough arguments in call to uuid.Must have (uuid.UUID) want (uuid.UUID, error)

    if I install the package using dep dep ensure -add github.com/satori/go.uuid, then Must function expects two arguments.

    uuid := uuid.Must(uuid.NewV4(), errors.New("UUID Error"))

    but if I install the package using go get github.com/satori/go.uuid, then Must function expects only 1 argument

    uuid := uuid.Must(uuid.NewV4())

    In both the cases 1.2 version is getting installed

  • Postgres Scan UUID

    Postgres Scan UUID

    This fixes a case when using the gorm "ORM" with go.uuid on a Postgres database. When a model defines a field as uuid.UUID the introspection that happens will pass in a UUID type.

  • Everything is broken

    Everything is broken

    After this sudden change, everything in my code will NOT work. Not even build with docker

    All I get is this multiple-value uuid.NewV4() in single-value context

  • Add Generator interface

    Add Generator interface

    In certain cases (namely testing) a person may want to deterministically generate UUIDs. There currently aren't many facilities in this UUID library for doing this.

    I propose introducing a new interface called Generator. A Generator has all of the standard uuid creation functions on it, but has the advantage of being able to be mocked. Developers wishing to write more testable code can write code that instead of directly using the creation function, instead uses some instance of the Generator interface. For the code they actually want to run, they can inject the Generator returned by the NewGenerator function. When they want to test, they can inject a mocked Generator of their choice (using mockgen, mockery, or their own custom implementation) and deterministically control what UUIDs are generated.

    For instance, consider the following generator that might be used for testing. In this case, the author only cares about deterministically generating a certain sequence of V4 UUIDS.

    type MyTestGen struct {
      Sequence []satori.UUID
      Iter            int
    }
    
    func (_ MyTestGen) NewV1() satori.UUID{return  satori.Nil}
    func (_ MyTestGen) NewV2(domain byte) satori.UUID{return  satori.Nil}
    func (_ MyTestGen) NewV3(ns UUID, name string) satori.UUID{return  satori.Nil}
    func (m MyTestGen) NewV4() satori.UUID{
      toReturn := m.Sequence[m.Iter]
      m.Iter += 1
      return toReturn
    }
    func (_ MyTestGen) NewV5(ns UUID, name string) satori.UUID{return  satori.Nil}
  • Add method to get UUID as string without dashes.

    Add method to get UUID as string without dashes.

    I need the possibility to have an UUID string without dashes in my project. I didn't want to use strings package to manage it since it could be a new simple functionality.

  • uuid.FromString(

    uuid.FromString("6ba7b8109dad11d180b400c04fd430c8") does not work, and really should.

    I feel the tests have an error in them, in that a format is not supported that really, really should be.

    While uuid.FromString("6ba7b810-9dad-11d1-80b4-00c04fd430c8") is clearly supported, the string uuid.FromString(""6ba7b8109dad11d180b400c04fd430c8") should also work as well as its considered a valid uuid in many languages and schema frameworks and so this support is important for interoperability... just it does not work at the moment even though it really should, and this has caused us (and I'm sure, many many others) all kinds of issues.

    For example, this lack of support for UUID's without dashes makes dealing with marshmallow and Python generated .hex UUID values a big PITA when you trying to use the hex value as a primary key in a DB or massively distributed system.

    It also means that while other libraries and languages are generating these "valid UUID's", the UUID framework itself does not support them, so it makes it look like this library doesn't have full support for UUID's.

    I understand that some may want to create an overly pedantic discussion about if a UUID without a dash is really a UUID that won't take into account the fact real people are using these libraries to get work done and so this support would make lives easier, but the fact is most people consider them to be valid, many other frameworks consider them to be valid, and so this support really should exist if the core golang philosophy of getting stuff done is to be respected.

  • Refactor UnmarshalText in order to parse hash-like UUID.

    Refactor UnmarshalText in order to parse hash-like UUID.

    I refactor UnmarshalText in order to support yet another text representation of UUID lilke 6ba7b8109dad11d180b400c04fd430c8. The reason is that this format is quite frequent.

    I tried to introduce BNF of what UmarshalText actually does. In fact it simplifies implementation and future support. On other hand current implementation led to performance degradation(see below before/after).

    BEFORE

    BenchmarkFromString-4               	10000000	       122 ns/op
    BenchmarkFromStringUrn-4            	10000000	       124 ns/op
    BenchmarkFromStringWithBrackets-4   	10000000	       123 ns/op
    BenchmarkUnmarshalText-4            	20000000	        81.6 ns/op
    PASS
    ok  	github.com/daskol/go.uuid	25.786s
    

    AFTER

    BenchmarkFromString-4               	20000000	        91.7 ns/op
    BenchmarkFromStringUrn-4            	 3000000	       493 ns/op
    BenchmarkFromStringWithBrackets-4   	 5000000	       358 ns/op
    BenchmarkUnmarshalText-4            	 2000000	       612 ns/op
    PASS
    ok  	github.com/daskol/go.uuid	27.822s
    

    Eventually correct UnmarshalText should be based on FSA to avoid overhead during parsing.

  • sql.Scan does not work for mssql driver

    sql.Scan does not work for mssql driver

    Hi,

    i am having a problem when scanning from MS SQL Server a uuid. The uuid from the app and the value in the db table is the same, but when querying and scan the result i get a different uuid. The same app does work in PostgreSQL just by switching out the driver. Any thoughts?

  • error value when doing rand.Read(...)

    error value when doing rand.Read(...)

    First of all thanks for a great library.

    This is more of a question rather than a real issue: what happens if rand.Read() fails? I realize in practice it probably doesn't fail much (and I've been running a script locally to try and catch a random failure for crypt.Rand() but haven't had any luck) -- but in that rare case what should be the behavior of the library?

    Maybe a retry works? If so I'll be happy to do a PR.

  • Can you tag the fix that is in master using ReadFull as 1.2.1?

    Can you tag the fix that is in master using ReadFull as 1.2.1?

    https://github.com/satori/go.uuid/issues/73#issuecomment-378573107 the use of ReadFull is now in master but has not been tagged. This is a critical fix for us, can you add a tag as version 1.2.1 (or 1.3). Thanks.

  • It's time to switch to gitee.com/gofrs/uuid

    It's time to switch to gitee.com/gofrs/uuid

    This gofrs has nothing to do with github.com/gofrs. I just want to urge them to support go module asap.

    Package main page: https://pkg.go.dev/gitee.com/gofrs/uuid/v4

    For Chinese friends suffering from poor network when connecting to GitHub, it's time.

  • Add Comb UUID

    Add Comb UUID

  • IMPORTANT: Unresolved CVE on latest release (CVE-2021-3538 )

    IMPORTANT: Unresolved CVE on latest release (CVE-2021-3538 )

    A CVEhas been filed for a 3 year old defect that is fixed on master but has never been tagged and released. With this defect, periodically the UUID V4s will contain mostly 0's.

    https://github.com/gofrs/uuid is a maintained fork that addresses this problem and is actively maintained.

    https://github.com/satori/go.uuid/issues/73

Related tags
UUID package for Golang

UUID package for Go language This package provides pure Go implementation of Universally Unique Identifier (UUID). Supported both creation and parsing

Dec 9, 2021
An extremely fast UUID alternative written in golang

Overview WUID is a globally unique number generator, while it is NOT a UUID implementation. WUID is 10-135 times faster than UUID and 4600 times faste

Dec 9, 2022
An extremely fast UUID alternative written in golang

Overview WUID is a globally unique number generator, while it is NOT a UUID implementation. WUID is 10-135 times faster than UUID and 4600 times faste

May 10, 2021
A lightweight UUID implementation

uuid uuid is a lightweight implementation for Univerally unique identifier. Usage var id UUID = uuid.Rand() fmt.Println(id.Hex()) fmt.Println(id.Raw()

Feb 25, 2020
Library to integrate github.com/google/uuid with gopkg.in/vmihailenco/msgpack

Library to integrate github.com/google/uuid with gopkg.in/vmihailenco/msgpack

Apr 26, 2022
A simple uuid library based on RFC 4122

UUID generator A simple library that generates uuids. Supported versions: version 1 version 3 version 4 version 5 Supported variants: DCE Microsoft Th

Oct 20, 2021
Generate UUID for script's sql

Generate UUID for script's sql Instale o go em sua maquina https://golang.org/doc/install Baixe as seguintes dependecias go get github.com/satori/go.u

Oct 26, 2021
Alternative random id generation to uuid

randid import "go.withmatt.com/randid" id := randid.New().String() randid's goals are simple: a smaller, more compact, faster version of uuid4. I don

Nov 16, 2022
A UUIDv4 generation package written in go

Goid A Go package to generate V4 UUIDs Documentation The API docs can be viewed here or generated with godoc. Usage An example of generating a v4 UUID

Dec 24, 2022
Go package for UUIDs based on RFC 4122 and DCE 1.1: Authentication and Security Services.

uuid The uuid package generates and inspects UUIDs based on RFC 4122 and DCE 1.1: Authentication and Security Services. This package is based on the g

Jan 1, 2023
A simple to use Go (golang) package to generate or parse Twitter snowflake IDs

snowflake snowflake is a Go package that provides A very simple Twitter snowflake generator. Methods to parse existing snowflake IDs. Methods to conve

Dec 30, 2022
✨ Generate unique IDs (Port of Node package "generate-snowflake" to Golang)

✨ Generate Snowflake Generate unique IDs. Inspired by Twitter's Snowflake system. ?? Installation Initialize your project (go mod init example.com/exa

Feb 11, 2022
Snowflake - A simple to use Go (golang) package to generate or parse Twitter snowflake IDs
Snowflake - A simple to use Go (golang) package to generate or parse Twitter snowflake IDs

❄️ Go-Snowflake A Snowflake Generator for Go A simple to use Go (golang) package

Oct 20, 2022
UUID package for Go

UUID package for Go language This package provides pure Go implementation of Universally Unique Identifier (UUID). Supported both creation and parsing

Jan 2, 2023
UUID package for Golang

UUID package for Go language This package provides pure Go implementation of Universally Unique Identifier (UUID). Supported both creation and parsing

Dec 9, 2021
An extremely fast UUID alternative written in golang

Overview WUID is a globally unique number generator, while it is NOT a UUID implementation. WUID is 10-135 times faster than UUID and 4600 times faste

Dec 9, 2022
An extremely fast UUID alternative written in golang

Overview WUID is a globally unique number generator, while it is NOT a UUID implementation. WUID is 10-135 times faster than UUID and 4600 times faste

May 10, 2021
A lightweight UUID implementation

uuid uuid is a lightweight implementation for Univerally unique identifier. Usage var id UUID = uuid.Rand() fmt.Println(id.Hex()) fmt.Println(id.Raw()

Feb 25, 2020
Library to integrate github.com/google/uuid with gopkg.in/vmihailenco/msgpack

Library to integrate github.com/google/uuid with gopkg.in/vmihailenco/msgpack

Apr 26, 2022
golang uuid-shellcode加载器,分离执行,可直接把shellcode写入程序。

golang uuid-shellcode加载器,分离执行,可直接把shellcode写入程序。

Jan 7, 2023