github.com/haraldrudell/parl@v0.4.176/unique-id-uint64.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  // UniqueIDUint64 generates executable-invocation-unique uint64 numbers 1…
    14  // Different series of uint64 from different generators does not have identity,
    15  // ie. they cannot be told apart.
    16  // Consider UniqueIDTypedUint64 to have identity.
    17  //
    18  // Usage:
    19  //
    20  //	var generator parl.UniqueIDUint64
    21  //	id := generator.ID()
    22  type UniqueIDUint64 uint64
    23  
    24  // ID generates a unique uint64 1…. thread-safe
    25  func (u *UniqueIDUint64) ID() (uniqueUint64 uint64) {
    26  	return atomic.AddUint64((*uint64)(u), 1)
    27  }
    28  
    29  func (u *UniqueIDUint64) String() (s string) {
    30  	return "IDu64:" + strconv.FormatUint(uint64(*u), 10)
    31  }