github.com/go-board/x-go@v0.1.2-0.20220610024734-db1323f6cb15/metadata/mdkey/generator.go (about)

     1  package mdkey
     2  
     3  import (
     4  	"github.com/go-board/x-go/xstrings"
     5  	"github.com/gofrs/uuid"
     6  )
     7  
     8  // RequestIDGenerator generate a unique request id in string format
     9  // 生成字符串类型的唯一请求ID
    10  type RequestIDGenerator interface {
    11  	RequestID() string
    12  }
    13  
    14  // RandomIDGenerator is a global id generator for generate unique request id with random way
    15  var RandomIDGenerator RequestIDGenerator = randomIdGenerator{}
    16  
    17  // UUIDGenerator is a global id generator for generate unique request id with random uuid way
    18  var UUIDGenerator RequestIDGenerator = uuidGenerator{}
    19  
    20  type randomIdGenerator struct{}
    21  
    22  func (randomIdGenerator) RequestID() string {
    23  	id, _ := xstrings.FastRandom(32)
    24  	return id
    25  }
    26  
    27  type uuidGenerator struct{}
    28  
    29  func (uuidGenerator) RequestID() string { return uuid.Must(uuid.NewV4()).String() }