Created
September 19, 2017 15:40
-
-
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
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 ( | |
"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) | |
} | |
} | |
} |
Author
xeoncross
commented
Sep 19, 2017
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment