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 ofguid
is relatively simple, and details can be found in the implementation source code.
Characters:
Type Characters
Numerical 0123456789
English abcdefghijklmnopqrstuvwxyz
Usage:
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:
func S(data ...[]byte) string
- 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 of7
bytes;PID
represents the process ID of the current machine, consisting of4
bytes;TimestampNano
represents the current nanosecond timestamp, consisting of12
bytes;Sequence
represents the concurrent safe sequence number of the current process, consisting of3
bytes;RandomString
represents a random string, consisting of6
bytes;
- 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 to2
input parameters, consisting of7
or14
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 a7
byte hash value through a hash method. TimestampNano
represents the current nanosecond timestamp, consisting of12
bytes;Sequence
represents the concurrent safe sequence number of the current process, consisting of3
bytes;RandomString
represents a random string, consisting of3
or10
bytes, that is:- If
1
custom parameter is given, the remaining bytes will be filled with random numbers, with a length of10
bytes; - If
2
custom parameters are given, the remaining bytes will be filled with random numbers, with a length of3
bytes;
- If
Benchmark
goos: darwin
goarch: amd64
pkg: github.com/gogf/gf/v2/util/guid
cpu: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
Benchmark_S
Benchmark_S-12 2665587 423.8 ns/op
Benchmark_S_Data_1
Benchmark_S_Data_1-12 2027568 568.2 ns/op
Benchmark_S_Data_2
Benchmark_S_Data_2-12 4352824 275.5 ns/op
PASS
Example 1, Basic Usage
package main
import (
"fmt"
"github.com/gogf/gf/v2/util/guid"
)
func main() {
fmt.Printf("TraceId: %s", guid.S())
}
After execution, the output will be:
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:
func CreateSessionId(r *ghttp.Request) string {
var (
address = request.RemoteAddr
header = fmt.Sprintf("%v", request.Header)
)
return guid.S([]byte(address), []byte(header))
}
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.