Generating UUIDs With Go

Universally Unique Identifiers (UUID) are popular for user identification in applications where a high degree of randomness is essential. UUIDs are a good choice for apps that serve millions of users since they reduce the chance of collisions and they’re universally distinct across systems.

Go doesn’t provide any package(s) for generating UUIDs. Since most Unix-related operating systems include theuuidgenprogram, you’re able to use theospackage to generate them. You can also use an external Go package like Google’suuidor thegouuidpackage.

4

Generating UUIDs With the os Package

The Goospackage provides functionality for interacting with the host machine’s operating system in a Unix-like fashion. You can execute commands with theexecsubpackage of theospackage and retrieve the output for yourGo programs.

Import these built-in packages in your Go file to generate UUIDs with theospackage.

The black silhouette of a person standing, looking up at a starry night sky with a background ranging from orange to turquoise

You’ll use thelogpackage to log possible errors andthefmtstring-formatting packageto output the generated UUID.

You can run commands with theCommandmethod of theexecpackage. TheOutputmethod returns the command’s output.

the os package overview

TheosExecfunction generates and prints a UUID using theuuidgencommand.

Generating UUIDs With Google’s UUID Package

The Googleuuidpackage generates and inspects UUIDs based onRFC 4122and the DCE 1.1 Authentication and Security Services.

Run this terminal command in your project’s directory to install theuuidpackage as one of your project’s dependencies.

google uuid preview

Import theuuidpackage to generate UUIDs and thefmtpackage to output the UUID to your console.

You can generate a UUID with the uuid package’sNewmethod. TheNewmethod returns a random UUID, and you can use itsStringmethod to obtain a string representation of it.

the gouuid preview

ThegoogleUUIDfunction uses theNewmethod to print the string format of a random UUID.

Generating UUIDs With the GoUUID Package

The gouuid packageprovides immutable UUID structs and functions for generating NewV3, NewV4, and NewV5 UUIDs and parsing the UUIDs as specified in RFC 4122.

Run this command in the terminal of your working directory to install thegouuidpackage to your project’s dependencies.

Import thefmtandgouuidpackages with the aliasuuid(conventional) to generate UUIDs.

You can generate version 4 UUIDs with theNewV4method that returns the UUID or an error.

ThegenerateV4method prints the generated UUID from theNewV4method of thegouuidpackage.

Similarly, you can generate version 5 UUIDs with theNewV5method. TheNewV5method takes in a UUID namespace URL and a byte slice and returns the generated version 5 UUID.

ThegenerateV5function prints the generated version five UUID after the UUID generation with theNewV5method if there are no generation errors.

You Can Generate UUIDs With Most Programming Languages

UUIDs are helpful for many use cases, so they’re popular in everyday applications. Most programming languages provide functionality for generating UUIDs in their standard libraries or ecosystem packages.

There are several ways to generate a UUID in Node.js, but why pick one over the other? Here’s why.

Anyone with more than a passing interest in motorsports must see these films.

Now, I actually finish the books I start.

you could’t call this offline, Notion.

The key is not to spook your friends with over-the-top shenanigans.

So much time invested, and for what?

Technology Explained

PC & Mobile