Skip to content

Instantly share code, notes, and snippets.

@PaluMacil
Created June 28, 2022 03:33
Show Gist options
  • Save PaluMacil/926d7ced6906c015adf2042d568fab52 to your computer and use it in GitHub Desktop.
Save PaluMacil/926d7ced6906c015adf2042d568fab52 to your computer and use it in GitHub Desktop.
package main
import (
"crypto/rand"
"encoding/base64"
"fmt"
)
type UUID []byte
func (uuid UUID) Base64() string {
return base64.URLEncoding.EncodeToString(uuid)
}
func (uuid UUID) String() string {
bytes := []byte(uuid)
return fmt.Sprintf("%x-%x-%x-%x-%x", bytes[0:4], bytes[4:6], bytes[6:8], bytes[8:10], bytes[10:])
}
func main() {
uuid := UUID(make([]byte, 16))
rand.Read(uuid)
fmt.Printf("bytes, (storage size) len 16: %d\n", uuid)
fmt.Printf("base64, len %d: %s\n", len(uuid.Base64()), uuid.Base64())
fmt.Printf("string, len %d: %s", len(uuid.String()), uuid)
}
@PaluMacil
Copy link
Author

bytes, (storage size) len 16: [16 162 10 71 21 16 37 138 163 238 203 173 61 142 43 249]
base64, len 24: EKIKRxUQJYqj7sutPY4r-Q==
string, len 36: 10a20a47-1510-258a-a3ee-cbad3d8e2bf9

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