UUID Introduction and Golang Implementation

The purpose of UUID is to enable all elements in the distributed system to have unique identification information without being specified through the central node, and without considering the problem of duplicate names when creating databases.

At present, the most widely used UUID is the RFC4122 protocol specification, and sometimes the GUID refers to the implementation of the standard UUID by Microsoft. In fact, one of the authors of RFC4122 is also a Microsoft employee.

The implementation methods of each version are briefly introduced as follows.

V1 Time based

It is generated through the current timestamp and the machine MAC address. Because the MAC address is globally unique, it can indirectly ensure that the UUID is globally unique. However, it exposes the MAC address of the computer and the time when the UUID was generated, which has been criticized.

V2 DCE Security

It is the same as the time-based UUID algorithm, but the first four positions of the timestamp will be replaced by the UID or GID of POSIX. However, this version is not explicitly specified in the UUID specification and will not be implemented.

V3 namespace based

The user specifies a namespace and a specific string, and then generates UUIDs through MD5 hashing. However, this version is described according to the specification, mainly for backward compatibility, so it is rarely used.

V4 is based on random number

UUIDs are generated according to random or pseudorandom numbers. This version is also used most intentionally or unintentionally.

V5 namespace based

It is similar to version 3, but the hash function is changed to SHA1.

Implementation and installation package of package based on Google

go get -u -v github.com/google/uuid
package main
import (
func strToMd5(data string) string {
    t := md5.New()
    io.WriteString(t, data)
    return fmt.Sprintf("%x", t.Sum(nil))
func main() {
    //V1 Time based
    u1, err := uuid.NewUUID()
    if err != nil {
   //Uuid performs md5 conversion
    u4 := uuid.New()
    fmt.Println("uuid v4结果", u4.String()) // a0d99f20-1dd1-459b-b516-dfeca4005203
    c := strToMd5(u4.String())
    fmt.Println("uuid做md5结果", c)

Leave a Reply

Your email address will not be published. Required fields are marked *