func (g *Gen) getClockSequence() (uint64, uint16, error) {
	var err error
	g.clockSequenceOnce.Do(func() {
		buf := make([]byte, 2)
		if _, err = io.ReadFull(g.rand, buf); err != nil {
			return
		}
		g.clockSequence = binary.BigEndian.Uint16(buf)
	})
	if err != nil {
		return 0, 0, err
	}
	g.storageMutex.Lock()
	defer g.storageMutex.Unlock()
	timeNow := g.getEpoch()
	if timeNow <= g.lastTime {
		g.clockSequence++
	}
	g.lastTime = timeNow
	return timeNow, g.clockSequence, nil
}
Returns the epoch and clock sequence.
package uuid
import (
	"crypto/md5"
	"crypto/rand"
	"crypto/sha1"
	"encoding/binary"
	"fmt"
	"hash"
	"io"
	"net"
	"os"
	"sync"
	"time"
)
const epochStart = 122192928000000000
type epochFunc func() time.Time
type HWAddrFunc func() (net.HardwareAddr, error)
var DefaultGenerator Generator = NewGen()
var (
	posixUID = uint32(os.Getuid())
	posixGID = uint32(os.Getgid())
)
func NewV1() (UUID, error) {
	return DefaultGenerator.NewV1()
}
func NewV2(domain byte) (UUID, error) {
	return DefaultGenerator.NewV2(domain)
}
func NewV3(ns UUID, name string) UUID {
	return DefaultGenerator.NewV3(ns, name)
}
func NewV4() (UUID, error) {
	return DefaultGenerator.NewV4()
}
func NewV5(ns UUID, name string) UUID {
	return DefaultGenerator.NewV5(ns, name)
}
type Generator interface {
	NewV1() (UUID, error)
	NewV2(domain byte) (UUID, error)
	NewV3(ns UUID, name string) UUID
	NewV4() (UUID, error)
	NewV5(ns UUID, name string) UUID
}
type Gen struct {
	clockSequenceOnce sync.Once
	hardwareAddrOnce  sync.Once
	storageMutex      sync.Mutex
	rand io.Reader
	epochFunc     epochFunc
	hwAddrFunc    HWAddrFunc
	lastTime      uint64
	clockSequence uint16
	hardwareAddr  [6]byte
}
var _ Generator = (*Gen)(nil)
func NewGen() *Gen {
	return NewGenWithHWAF(defaultHWAddrFunc)
}
func NewGenWithHWAF(hwaf HWAddrFunc) *Gen {
	return &Gen{
		epochFunc:  time.Now,
		hwAddrFunc: hwaf,
		rand:       rand.Reader,
	}
}
func (g *Gen) NewV1() (UUID, error) {
	u := UUID{}
	timeNow, clockSeq, err := g.getClockSequence()
	if err != nil {
		return Nil, err
	}
	binary.BigEndian.PutUint32(u[0:], uint32(timeNow))
	binary.BigEndian.PutUint16(u[4:], uint16(timeNow>>32))
	binary.BigEndian.PutUint16(u[6:], uint16(timeNow>>48))
	binary.BigEndian.PutUint16(u[8:], clockSeq)
	hardwareAddr, err := g.getHardwareAddr()
	if err != nil {
		return Nil, err
	}
	copy(u[10:], hardwareAddr)
	u.SetVersion(V1)
	u.SetVariant(VariantRFC4122)
	return u, nil
}
func (g *Gen) NewV2(domain byte) (UUID, error) {
	u, err := g.NewV1()
	if err != nil {
		return Nil, err
	}
	switch domain {
	case DomainPerson:
		binary.BigEndian.PutUint32(u[:], posixUID)
	case DomainGroup:
		binary.BigEndian.PutUint32(u[:], posixGID)
	}
	u[9] = domain
	u.SetVersion(V2)
	u.SetVariant(VariantRFC4122)
	return u, nil
}
func (g *Gen) NewV3(ns UUID, name string) UUID {
	u := newFromHash(md5.New(), ns, name)
	u.SetVersion(V3)
	u.SetVariant(VariantRFC4122)
	return u
}
func (g *Gen) NewV4() (UUID, error) {
	u := UUID{}
	if _, err := io.ReadFull(g.rand, u[:]); err != nil {
		return Nil, err
	}
	u.SetVersion(V4)
	u.SetVariant(VariantRFC4122)
	return u, nil
}
func (g *Gen) NewV5(ns UUID, name string) UUID {
	u := newFromHash(sha1.New(), ns, name)
	u.SetVersion(V5)
	u.SetVariant(VariantRFC4122)
	return u
}