Created
June 28, 2022 03:33
-
-
Save PaluMacil/926d7ced6906c015adf2042d568fab52 to your computer and use it in GitHub Desktop.
My simple UUID suggestion made originally in https://www.reddit.com/r/golang/comments/vm51cr/is_it_bad_to_use_short_20_chars_random_strings_as/
This file contains hidden or 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 ( | |
"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) | |
} |
Author
PaluMacil
commented
Jun 28, 2022
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment