2

I am trying to generate a UUID in Go in a specific pattern. The program that I am working on requires various uuids to be generated similar to an existing uuid string. So the program would read an existing uuid (whose format can change in the future) and generate a new uuid in the same format to replace the existing one.

I have used the "github.com/satori/go.uuid" package to generate an uuid. The sample code that I am using is below which I found online.

As multiple instances of this programs would be deployed in parallel, I would like to avoid collisions or duplication in the uuids generated.

    package main

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

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

    // or error handling
    u2:= uuid.NewV4()

    fmt.Printf("UUIDv4: %s\n", u2)

    /* Formats needed: 
    2286664c688130096c9ce9008e4d97fb
    6abb173a-b134-49f2-aa88-9dff5dab12a1 (This is obtained from the above code)
    C8-3C-9C-64-61-70-62-B9-34-AC-9A-20-C9-EF-1D-6D
    */
}
4
  • 1
    Generate UUIDs from a single source. Relying on multiple sources is asking for trouble, unless you want to add some unique "salt" to each source. Commented Jul 22, 2019 at 23:58
  • When you say multiple instances will be run, do you mean on different systems? Use uuid.NewV1() to guarantee different UUIDs on different systems (based on MAC address). Otherwise use NewV3() and make sure you pass a unique name. Commented Jul 23, 2019 at 2:23
  • 1
    BTW Your formatting requirements are just a matter of getting dashes in the right place, using string slice operations. Commented Jul 23, 2019 at 2:27
  • for UUID i think not might be unique if you want to run parallel, solution for that you can use another combination to make it unique and the ID to be quite long char (if the spec can modify) Commented Jul 23, 2019 at 11:56

1 Answer 1

4

UUIDs are 128 bits; that's a space large enough that collisions are staggeringly unlikely.

From https://en.wikipedia.org/wiki/Universally_unique_identifier:

When generated according to the standard methods, UUIDs are for practical purposes unique, without depending for their uniqueness on a central registration authority or coordination between the parties generating them, unlike most other numbering schemes. While the probability that a UUID will be duplicated is not zero, it is close enough to zero to be negligible.

Thus, anyone can create a UUID and use it to identify something with near certainty that the identifier does not duplicate one that has already been, or will be, created to identify something else. Information labeled with UUIDs by independent parties can therefore be later combined into a single database or transmitted on the same channel, with a negligible probability of duplication.

The whole point of UUIDs, the reason they are so large, is to avoid collisions without synchronization. Per the same article, in order to reach a 50% chance of a collision would require:

generating 1 billion UUIDs per second for about 85 years, and a file containing this many UUIDs, at 16 bytes per UUID, would be about 45 exabytes, many times larger than the largest databases currently in existence, which are on the order of hundreds of petabytes.

Sign up to request clarification or add additional context in comments.

1 Comment

Thankyou Adrian, your answer helped. I also generated an uuid and reformatted it into the required way which is the second part of the solution for my problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.