SoFunction
Updated on 2025-03-05

Implementation of generating a unique UUID identification in GoLang

1. What is UUID

Introduction to UUID

Universally Unique Identifier (UUID) is a software construction standard and is also part of the Free Software Foundation organization in the field of decentralized computing environments.

The purpose of UUID is to enable all elements in a decentralized system to have unique identification information without specifying identification information through the central control end.

This way, everyone can create a UUID that does not conflict with others.

In this case, there is no need to consider the duplication of names when the database is created. The most widely used UUID is Microsoft's global unique identifier (GUID), while other important applications include Linux ext2/ext3 file system, LUKS encrypted partition, GNOME, KDE, Mac OS X, etc.

2. The existing third-party generation method of UUID in Go

At present, uuid in golang has not been included in the standard library. We use the open source library on github:

go get -u /satori/

use:

package main 
import (
    "fmt"
    "/satori/"
)
 
func main() {
   id := uuid.NewV4()
   ids := ()
}

3. Custom UUIDGenerator implementation

Function:

UUIDGenerator can generate a globally unique string ID. The ID consists of two parts, one is a prefix specified by the user, and the other is a number, with the maximum value of the number being 4294967295;

UUIDGenerator can generate globally unique unsigned shaping numbers, with numerical range 0-4294967295

Code

package utils 
import "fmt"
 
const (
    MAXUINT32 = 4294967295
    DEFAULT_UUID_CNT_CACHE = 512
)
 
type UUIDGenerator struct {
    Prefix       string
    idGen        uint32
    internalChan chan uint32
}
 
func NewUUIDGenerator(prefix string) *UUIDGenerator {
    gen := &UUIDGenerator{
        Prefix:       prefix,
        idGen:        0,
        internalChan: make(chan uint32, DEFAULT_UUID_CNT_CACHE),
    }
    ()
    return gen
}
 
//Open goroutine, put the generated digital UUID into the buffer pipelinefunc (this *UUIDGenerator) startGen() {
    go func() {
        for {
            if  == MAXUINT32 {
                 = 1
            } else {
                 += 1
            }
             <- 
        }
    }()
}
 
//Get the UUID in the form of a prefixed stringfunc (this *UUIDGenerator) Get() string {
    idgen := <-
    return ("%s%d", , idgen)
}
 
//Get UUID in uint32 formfunc (this *UUIDGenerator) GetUint32() uint32 {
    return <-
}

Test cases:

package utils 
import (
    "testing"
    "fmt"
)
 
func TestUUIDGenerator(t *) {
    // Create a new UUIDGennerator    UUIDFactory := NewUUIDGenerator("idtest")
 
    //Get UUID    for i:= 0; i < 50; i++{
        (())
    }
 
    //Get the UUID in the form of uint32    for i := 0; i < 50; i++{
        (UUIDFactory.GetUint32())
    }
}

result

idtest1

idtest2

idtest3

idtest4

idtest5

6

7

8

9

10

PASS

Supplement: UUID usage of golang

Install

go get /satori/

use

Several ways of generating UUIDs:

Version 1, based on timestamp and MAC address (RFC 4122)
Version 2, based on timestamp, MAC address and POSIX UID/GID (DCE 1.1)
Version 3, based on MD5 hashing (RFC 4122)
Version 4, based on random numbers (RFC 4122)
Version 5, based on SHA-1 hashing (RFC 4122)

Sample code:

package main
import (
    "fmt"
    "/satori/"
)
func main() {
    // Creating UUID Version 4
    // panic on error
    u1 := (uuid.NewV4())
    ("UUIDv4: %s\n", u1)
    // or error handling
    u2, err := uuid.NewV4()
    if err != nil {
        ("Something went wrong: %s", err)
        return
    }
    ("UUIDv4: %s\n", u2)
    // Parsing UUID from string input
    u2, err := ("6ba7b810-9dad-11d1-80b4-00c04fd430c8")
    if err != nil {
        ("Something went wrong: %s", err)
    }
    ("Successfully parsed: %s", u2)
}

The above is personal experience. I hope you can give you a reference and I hope you can support me more. If there are any mistakes or no complete considerations, I would like to give you advice.