guid provides a more convenient and higher performance global unique number generation feature. The generated uid string only includes numbers and lowercase English characters.

  • Advantages: High performance, easy to use.
  • Disadvantages: Limited character range, fixed length of 32 bytes.

The purpose of designing the guid module is to provide a more convenient, higher performance unique number generation that can meet the requirements of most business scenarios. The design of guid is relatively simple, and details can be found in the implementation source code.

Characters:

  1. Type Characters
  2. Numerical 0123456789
  3. English abcdefghijklmnopqrstuvwxyz

Usage:

  1. import "github.com/gogf/gf/v2/util/guid"

API Documentation:

https://pkg.go.dev/github.com/gogf/gf/v2/util/guid

Introduction

guid generates a 32 byte unique number through the S method, which is defined as follows:

  1. func S(data ...[]byte) string
  1. When used without any parameters, the unique number generated by this method will be composed as follows:

MACHash(7) + PID(4) + TimestampNano(12) + Sequence(3) + RandomString(6)

Where:

  • MAC represents the MAC address hash of the current machine, consisting of 7 bytes;
  • PID represents the process ID of the current machine, consisting of 4 bytes;
  • TimestampNano represents the current nanosecond timestamp, consisting of 12 bytes;
  • Sequence represents the concurrent safe sequence number of the current process, consisting of 3 bytes;
  • RandomString represents a random string, consisting of 6 bytes;
  1. When using any custom parameters, the unique number generated by this method will be composed as follows:

DataHash(7/14) + TimestampNano(12) + Sequence(3) + RandomString(3/10)

Main Points:

  • Data represents custom parameters, with a type of []byte, supporting up to 2 input parameters, consisting of 7 or 14 bytes;
  • Note that the input custom parameters need to have some unique identification in the business context, making the generated unique number more valuable;
  • Regardless of the length of each []byte parameter, they will eventually generate a 7 byte hash value through a hash method.
  • TimestampNano represents the current nanosecond timestamp, consisting of 12 bytes;
  • Sequence represents the concurrent safe sequence number of the current process, consisting of 3 bytes;
  • RandomString represents a random string, consisting of 3 or 10 bytes, that is:
    • If 1 custom parameter is given, the remaining bytes will be filled with random numbers, with a length of 10 bytes;
    • If 2 custom parameters are given, the remaining bytes will be filled with random numbers, with a length of 3 bytes;

Benchmark

  1. goos: darwin
  2. goarch: amd64
  3. pkg: github.com/gogf/gf/v2/util/guid
  4. cpu: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
  5. Benchmark_S
  6. Benchmark_S-12 2665587 423.8 ns/op
  7. Benchmark_S_Data_1
  8. Benchmark_S_Data_1-12 2027568 568.2 ns/op
  9. Benchmark_S_Data_2
  10. Benchmark_S_Data_2-12 4352824 275.5 ns/op
  11. PASS

Example 1, Basic Usage

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/gogf/gf/v2/util/guid"
  5. )
  6. func main() {
  7. fmt.Printf("TraceId: %s", guid.S())
  8. }

After execution, the output will be:

  1. TraceId: oa9sdw03dk0c35q9bdwcnz42p00trwfr

Example 2, Custom Parameters

Our SessionId generation needs to have good uniqueness and prevent easy collisions, so the following method can be used:

  1. func CreateSessionId(r *ghttp.Request) string {
  2. var (
  3. address = request.RemoteAddr
  4. header = fmt.Sprintf("%v", request.Header)
  5. )
  6. return guid.S([]byte(address), []byte(header))
  7. }

As you can see, SessionId relies on two custom input parameters RemoteAddr and Header to generate, and these two parameters have a certain unique identification in the business context. The design of the guid.S method ensures that the generated unique number will be extremely random and unique, meeting business needs and ensuring safety.