Skip to content

Instantly share code, notes, and snippets.

@xeoncross
Created September 19, 2017 15:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xeoncross/bc6165b70176de5d490e4c1e0a03516f to your computer and use it in GitHub Desktop.
Save xeoncross/bc6165b70176de5d490e4c1e0a03516f to your computer and use it in GitHub Desktop.
How easily would a collision appear if you had two goroutines creation nanosecond timestamps? Is this a good idea for unique ID generation that would fit in an int64? https://play.golang.org/p/ow0QYTMa30
package main
import (
"log"
"time"
)
func main() {
var counter int
one := make(chan int64)
two := make(chan int64)
go func() {
for {
one <- time.Now().UnixNano()
}
}()
go func() {
for {
two <- time.Now().UnixNano()
}
}()
// Read both values
for a := range one {
counter++
b := <-two
if a == b {
log.Fatalf("Match: %d (%d tries)", a, counter)
}
}
}
@xeoncross
Copy link
Author

xeoncross commented Sep 19, 2017

$ go run unixnano.go
2017/06/19 07:40:42 Match: 1505835642386934000 (588843 tries)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment