Tutorial for generating UUID in Go language and corresponding source code analysis

a background

      In a demand project developed by our company, we encountered a problem, that is, when the generated message is stored in the table, it must be distinguished from other messages. At that time, I thought of directly using the primary key ID to distinguish, which is the most concise. Colleagues said that it can also be used UUIDto distinguish. So I studied how to implement it based on Golang UUID.

Two UUID related knowledge

2.1 What is UUID and related introduction

      Excerpted from Wikipedia: The purpose of UUID is to allow all elements in a distributed system to have convenient identification information to represent a unique serial number, without having to consider the problem of duplication of names when creating a database. When UUID is generated according to a standard method, it is unique in practical applications and does not rely on registration and allocation by a central authority. The probability of UUID duplication is close to zero and can be ignored. In its canonical text representation, the 16 octets of a UUID are represented as 32 hexadecimal digits, displayed in five groups separated by a hyphen '-', in the form "8-4-4-4-12" for a total of 36 characters (32 hexadecimal digits and 4 hyphens). For example:

123e4567-e89b-12d3-a456-426655440000
xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx

The four-digit number M indicates the UUID version, and the one to three most significant digits of the number N indicate the UUID variant. In the example, M is 1 and N is a (10xx2), which means this UUID is a "variant 1", "version 1" UUID; that is, a time-based DCE/RFC 4122 UUID.

      normative8-4-4-4-12

Guess you like

Origin blog.csdn.net/whq19890827/article/details/128226597