How to generate UUID in Go? No built-in function?
Hello everyone! I've just started learning Go and came across a task: I need to generate unique identifiers (UUID) for database records. In Python I'm used to using `uuid.uuid4()`, but in Go, no matter how much I searched the standard library, I couldn't find anything similar. Only `crypto/rand` for random numbers and `fmt.Sprintf` for formatting.
I tried to write my own implementation — just generate 16 random bytes and manually insert hyphens like in the standard UUID format. But first, it looks messy, and second, I'm not sure the bytes will conform to UUID version 4 (random). My code:
```go
package main
import (
"crypto/rand"
"fmt"
)
func main() {
b := make([]byte, 16)
_, err := rand.Read(b)
if err != nil {
panic(err)
```
No answers yet.
Log in to leave an answer