Created
September 11, 2018 19:51
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
Author
xeoncross
commented
Sep 11, 2018
•
- https://github.com/lpar/serial/blob/master/serial.go (with checks to ensure each number is larger than the last)
- https://gist.github.com/rbastic/304e2a0e50db8d315ff7c85d817f4872
- https://github.com/golang/proposal/blob/master/design/12914-monotonic.md
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment