Cookies management by TermsFeed Cookie Consent

🆔 Generate UUID in Go

id

UUID (Universally Unique Identifier) is a 128-bit value used to uniquely identify information in computer systems. UUIDs are generated using algorithms that guarantee that each of them is unique.

They are commonly used in programming languages to uniquely identify objects or data. UUIDs can be represented in various formats, but the most commonly used is the hyphenated format, which looks like this:

53aa35c8-e659-44b2-882f-f6056e443c99

In Go, there are many packages with which you can generate a UUID. Some of the more popular ones are github.com/google/uuid and github.com/gofrs/uuid. Both of these are based on RFC 4122 specification and support UUID generation in various versions. In this article, we provide examples of how to generate UUIDs with them in the default way, as well as using more advanced UUID version selection options.

Generate UUID using the github.com/google/uuid package

To generate a UUID using the github.com/google/uuid package, you must first download and install it using the go get Terminal command:

go get github.com/google/uuid

Now, you can use this package in your code:

package main

import (
    "fmt"

    "github.com/google/uuid"
)

func main() {
    uuid := uuid.NewString()
    fmt.Println(uuid)
}

The uuid.NewString() is the simplest function to generate UUID using the github.com/google/uuid package. It returns a random (Version 4) UUID as a string ready to use or print.

UUIDs are standardized and come in many variants and versions. If you want to know them all, a good place to start is to read the Wikipedia article here. Different versions of UUIDs are based on different information:

  • Version 1 UUIDs are generated from a time and the MAC address.
  • Version 2 UUIDs are generated from an identifier (usually user ID), time, and the MAC address.
  • Version 3 and 5 UUIDs are generated based on hashing a namespace identifier and name.
  • Version 4 UUIDs are based on a randomly generated number.

After running the application, you will see a randomly generated UUID in the output:

8cfb429e-0220-42a9-919c-54d6886d5128

Generate UUID using the github.com/gofrs/uuid package

To get and install the github.com/gofrs/uuid package, you need to execute the command below:

go get github.com/gofrs/uuid

After that, you can generate UUID using this package in your app:

package main

import (
    "fmt"
    "log"

    "github.com/gofrs/uuid"
)

func main() {
    uuid, err := uuid.NewV4()
    if err != nil {
        log.Fatalf("failed to generate UUID: %v", err)
    }
    fmt.Println(uuid)
}

The example above shows how to generate version 4 UUID. All you need to do is use the uuid.NewV4() function.

As before, the output of the app is a randomly generated ID:

a40d6058-069e-4a7a-b873-41e3bff827be

Generate a UUID of a specific version

If you are curious about how you can generate a UUID in a different version than the most popular Version 4, here is how you can do it using both packages.

With the github.com/google/uuid package

package main

import (
    "fmt"
    "log"

    "github.com/google/uuid"
)

func main() {
    // version 1 uuid
    v1, err := uuid.NewUUID()
    if err != nil {
        log.Fatal("cannot generate v1 uuid")
    }
    fmt.Printf("v1 uuid: %v\n", v1)

    // version 2 uuid
    v2, err := uuid.NewDCEGroup()
    if err != nil {
        log.Fatal("cannot generate v2 uuid")
    }
    fmt.Printf("v2 uuid: %v\n", v2)

    // version 3 uuid
    v3 := uuid.NewMD5(uuid.NameSpaceURL, []byte("https://example.com"))
    fmt.Printf("v3 uuid: %v\n", v3)

    // version 4 uuid
    v4, err := uuid.NewRandom()
    if err != nil {
        log.Fatal("cannot generate v4 uuid")
    }
    fmt.Printf("v4 uuid: %v\n", v4)

    // version 5 uuid
    v5 := uuid.NewSHA1(uuid.NameSpaceURL, []byte("https://example.com"))
    fmt.Printf("v5 uuid: %v\n", v5)
}

With the github.com/gofrs/uuid package

package main

import (
    "fmt"
    "log"

    "github.com/gofrs/uuid"
)

func main() {
    // version 1 uuid
    v1, err := uuid.NewV1()
    if err != nil {
        log.Fatal("cannot generate v1 uuid")
    }
    fmt.Printf("v1 uuid: %v\n", v1)

    // version 3 uuid
    v3 := uuid.NewV3(uuid.NamespaceURL, "https://example.com")
    fmt.Printf("v3 uuid: %v\n", v3)

    // version 4 uuid
    v4, err := uuid.NewV4()
    if err != nil {
        log.Fatal("cannot generate v4 uuid")
    }
    fmt.Printf("v4 uuid: %v\n", v4)

    // version 5 uuid
    v5 := uuid.NewV5(uuid.NamespaceURL, "https://example.com")
    fmt.Printf("v5 uuid: %v\n", v5)
}

Note that the github.com/gofrs/uuid package does not support UUID generation in version 2, but instead supports experimental versions 6 and 7 (not present in the example above).

Which UUID version to use?


Thank you for being on our site 😊. If you like our tutorials and examples, please consider supporting us with a cup of coffee and we'll turn it into more great Go examples.

Have a great day!

Buy Me A Coffee