github.com/haraldrudell/parl@v0.4.176/unique-id.go (about)

     1  /*
     2  © 2022–present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/)
     3  ISC License
     4  */
     5  
     6  package parl
     7  
     8  import (
     9  	"strconv"
    10  	"sync/atomic"
    11  )
    12  
    13  // UniqueID is an executable-invocation-unique identifier generator.
    14  // The format is a named-type small-integer numeric string suitable to distinguish multiple instances of a type.
    15  // The type is ordered and can be converted to string.
    16  //
    17  // Usage:
    18  //
    19  //	type MyType string
    20  //	var generator parl.UniqueID[MyType]
    21  //	someID := generator.ID()
    22  type UniqueID[T ~string] uint64
    23  
    24  // ID generates a unique string identifier. thread-safe
    25  func (u *UniqueID[T]) ID() (unique T) {
    26  	return T(strconv.FormatUint(atomic.AddUint64((*uint64)(u), 1), 10))
    27  }