github.com/angenalZZZ/gofunc@v0.0.0-20210507121333-48ff1be3917b/data/id/snowid/snowid_helper.go (about)

     1  package snowid
     2  
     3  import (
     4  	"sync"
     5  )
     6  
     7  var singletonMutex sync.Mutex
     8  var idGenerator *DefaultIdGenerator
     9  
    10  // SetDefaultIdGenerator Set default options.
    11  func SetDefaultIdGenerator(options *IdGeneratorOptions) {
    12  	singletonMutex.Lock()
    13  	idGenerator = NewDefaultIdGenerator(options)
    14  	singletonMutex.Unlock()
    15  }
    16  
    17  // NextId Create a new ID.
    18  func NextId() uint64 {
    19  	if idGenerator == nil {
    20  		singletonMutex.Lock()
    21  		defer singletonMutex.Unlock()
    22  		options := NewIdGeneratorOptions(1)
    23  		idGenerator = NewDefaultIdGenerator(options)
    24  	}
    25  
    26  	return idGenerator.NewLong()
    27  }