Generate UUID in Golang

Learn via video courses
Topics Covered

Overview

We often see that in our university everyone has their enrollment number, which helps us to find information related to a particular student e.g. student mark sheet, fee information, and so on. This helps us to identify identities very easily. The same feature also exists in Golang for different purposes, and this post will cover everything related to Golang UUID or GUUID, which helps to generate unique IDs every time.

Introduction

UUID is an abbreviation for Universally Unique Identifier. It is a 128-bit value used for Foundation identification (OSF).

UUIDs are unique as they produce or create unique IDs always which are generally based on time, mouse movement, system hardware, etc. As a result, UUIDs are very likely to be unique across space and time and extremely difficult to anticipate because they depend on various dependent on a variety of characteristics. The UUID package creates and inspects UUIDs with RFC 4122 and DCE 1.1: Authentication and Security Services.

How to Generate a UUID in Go

There are numerous libraries available, let us look at three libraries to generate for the same.

Steps to Generate the UUID:

Using github.com/satori/go.uuid

The first step is to install the package with the go command:

Note: If you find any error while running the above command you can run this command:

Steps to generate the UUID

Generate UUIDs

Output

Using https://github.com/pborman/uuid

The first step is to install the package with the go command:

Generating UUIDs:

Output

Using an Inbuilt Library

Using os/exec

Output

Using the UUID Package by Google

Steps to generate id through google package:

The first step is to install the package with the go command:

Using the UUID package by google

Generating UUIDs:

Output

Advantages of UUID

  • It is generally unique across all tables, databases, and servers.
  • It enables the simple mixing of information from several databases.
  • It enables simple database deployment across several hosts.
  • UUID/GUID fields are required in the majority of replication cases.
  • UUIDs are generated only once and cannot be modified, making them suitable for use as identifiers for immutable objects such as files or database records.
  • It is also useful in hashing algorithms

Disadvantages of UUID

  • It is four times larger than the standard four-byte index value.
  • It is more difficult to debug. Consider the SQL query WHERE user id='12e986e5-7b58-4e0b-safe-e78c081e8410'.
  • Because it is not sequential, new records are put at random points.
  • Because UUIDs are generated based on a combination of factors, they can be less performant compared to other ID types, such as auto-incrementing integers.
  • Sorting UUIDs can be more complex than sorting other ID types, as they may not be naturally ordered.
  • Some systems may not support UUIDs or may have limited support for them, which can make it difficult to use them in certain environments.
  • They are not always human-readable, which can make them difficult to work with in certain contexts.
  • They are not guaranteed to be unique across different systems, which can cause issues when trying to merge data from different sources.

When Should You Use UUIDS

  • When you don't want the user to be able to predict the IDs. For example, if there is /user/100, there must also be /user/99, and so on.
  • When you need to protect your database's record count. For example, suppose you have a user who has registered on your site and whose profile URL is /user/100. As a result, he may believe that he is the 100th user to register on the site.
  • When you don't want a database collision on countless cases.
  • When you need to use some of the benefits mentioned above.

Conclusion

  • In this article, we have used 3 different libraries to generate UUIDs in golang.
  • First, we have used github.com/satori/go.uuid, which is a simple and lightweight library to generate UUIDs.
  • Second, we have used github.com/pborman/uuid, which is another library to generate UUIDs, but it provides additional functionalities like generating UUIDs based on different versions.
  • Third, we have used an inbuilt library os/exec to generate UUIDs using the uuidgen command.
  • You can choose any of the above libraries based on your project requirements.
  • We have seen how to generate UUIDs in Golang with the help of various libraries and inbuilt commands. UUIDs are very useful in various scenarios where we need to generate unique IDs, like generating session IDs, tracking, identification, etc.