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
32bytes.
The purpose of designing the
guidmodule is to provide a more convenient, higher performance unique number generation that can meet the requirements of most business scenarios. The design ofguidis 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:
MACrepresents the MAC address hash of the current machine, consisting of7bytes;PIDrepresents the process ID of the current machine, consisting of4bytes;TimestampNanorepresents the current nanosecond timestamp, consisting of12bytes;Sequencerepresents the concurrent safe sequence number of the current process, consisting of3bytes;RandomStringrepresents a random string, consisting of6bytes;
- 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:
Datarepresents custom parameters, with a type of[]byte, supporting up to2input parameters, consisting of7or14bytes;- 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
[]byteparameter, they will eventually generate a7byte hash value through a hash method. TimestampNanorepresents the current nanosecond timestamp, consisting of12bytes;Sequencerepresents the concurrent safe sequence number of the current process, consisting of3bytes;RandomStringrepresents a random string, consisting of3or10bytes, that is:- If
1custom parameter is given, the remaining bytes will be filled with random numbers, with a length of10bytes; - If
2custom parameters are given, the remaining bytes will be filled with random numbers, with a length of3bytes;
- 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.