zergon321

Golang UUID string representation

Mar 25th, 2022 (edited)
519
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 2.46 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "encoding/base32"
  5.     "encoding/base64"
  6.     "encoding/hex"
  7.     "fmt"
  8.  
  9.     "github.com/btcsuite/btcutil/base58"
  10.     "github.com/google/uuid"
  11. )
  12.  
  13. // Example output:
  14. // Binary: [52 155 71 198 73 40 76 110 178 168 24 215 190 191 78 205]
  15. // Binary text: [51 52 57 98 52 55 99 54 45 52 57 50 56 45 52 99 54 101 45 98 50 97 56 45 49 56 100 55 98 101 98 102 52 101 99 100]
  16. // Text: 349b47c6-4928-4c6e-b2a8-18d7bebf4ecd
  17. // Hex binary: [51 52 57 98 52 55 99 54 52 57 50 56 52 99 54 101 98 50 97 56 49 56 100 55 98 101 98 102 52 101 99 100]
  18. // Hex: 349b47c649284c6eb2a818d7bebf4ecd
  19. // base58 binary: [55 86 109 112 104 117 77 116 76 122 69 122 50 51 100 66 65 99 105 118 106 87]
  20. // base58: 7VmphuMtLzEz23dBAcivjW
  21. // base64 binary: [78 74 116 72 120 107 107 111 84 71 54 121 113 66 106 88 118 114 57 79 122 81 61 61]
  22. // base64: NJtHxkkoTG6yqBjXvr9OzQ==
  23. // base32 binary: [71 83 78 85 80 82 83 74 70 66 71 71 53 77 86 73 68 68 76 51 53 80 50 79 90 85 61 61 61 61 61 61]
  24. // base32: GSNUPRSJFBGG5MVIDDL35P2OZU======
  25. // Length of the guid in binary: 16
  26. // Length of the base58 representation of the guid in binary: 22
  27. // Length of the base64 representation of the guid in binary: 24
  28. // Length of the base32 representation of the guid in binary: 32
  29.  
  30. func main() {
  31.     guid := uuid.New()
  32.     guidBin, err := guid.MarshalBinary()
  33.     handleError(err)
  34.     guidStr, err := guid.MarshalText()
  35.     handleError(err)
  36.     guidHex := hex.EncodeToString(guidBin)
  37.     guidBase58 := base58.Encode(guidBin)
  38.     guidBase64 := base64.StdEncoding.EncodeToString(guidBin)
  39.     guidBase32 := base32.StdEncoding.EncodeToString(guidBin)
  40.  
  41.     fmt.Println("Binary:", guidBin)
  42.     fmt.Println("Binary text:", guidStr)
  43.     fmt.Println("Text:", string(guidStr))
  44.     fmt.Println("Hex binary:", []byte(guidHex))
  45.     fmt.Println("Hex:", guidHex)
  46.     fmt.Println("base58 binary:", []byte(guidBase58))
  47.     fmt.Println("base58:", guidBase58)
  48.     fmt.Println("base64 binary:", []byte(guidBase64))
  49.     fmt.Println("base64:", guidBase64)
  50.     fmt.Println("base32 binary:", []byte(guidBase32))
  51.     fmt.Println("base32:", guidBase32)
  52.     fmt.Println("Length of the guid in binary:", len(guidBin))
  53.     fmt.Println("Length of the base58 representation of the guid in binary:", len([]byte(guidBase58)))
  54.     fmt.Println("Length of the base64 representation of the guid in binary:", len([]byte(guidBase64)))
  55.     fmt.Println("Length of the base32 representation of the guid in binary:", len([]byte(guidBase32)))
  56. }
  57.  
  58. func handleError(err error) {
  59.     if err != nil {
  60.         panic(err)
  61.     }
  62. }
  63.  
Add Comment
Please, Sign In to add comment