Skip to content

Instantly share code, notes, and snippets.

@pedrogardim
Created October 5, 2023 13:33
Show Gist options
  • Save pedrogardim/1a5559a47b49d0c3b05ae6f858d68285 to your computer and use it in GitHub Desktop.
Save pedrogardim/1a5559a47b49d0c3b05ae6f858d68285 to your computer and use it in GitHub Desktop.
Golang | Generate random MongoDB ObjectId
func mongoObjectId() string {
ts := time.Now().UnixMilli() / 1000;
id := strconv.FormatInt(ts, 16)
for i := 0; i < 16; i++ {
id += fmt.Sprintf("%x", rand.Intn(16))
}
return id
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment