Skip to content

Instantly share code, notes, and snippets.

@xeoncross
Created September 11, 2018 19:51
Show Gist options
  • Save xeoncross/ed34985b3d9d00a87a352f5e7acd61bd to your computer and use it in GitHub Desktop.
Save xeoncross/ed34985b3d9d00a87a352f5e7acd61bd to your computer and use it in GitHub Desktop.
Trying to get a unique ID per instance by using UnixNano + a mutex lock: https://play.golang.org/p/q8OzAXzha3B
package main
import (
"fmt"
"sync"
"time"
)
type Sequence struct {
m sync.Mutex
}
func (s *Sequence) Next() int64 {
s.m.Lock()
defer s.m.Unlock()
return time.Now().UnixNano()
}
func main() {
s := Sequence{}
for i := 0; i < 10; i++ {
go func() {
fmt.Println(s.Next())
}()
}
time.Sleep(time.Millisecond)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment