Created
May 1, 2020 18:37
-
-
Save saurcery/a7312588da7545bcd4f70b1dbf2f2d9b to your computer and use it in GitHub Desktop.
This file contains 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 ( | |
"fmt" | |
"github.com/gofrs/uuid" | |
"encoding/base64" | |
"encoding/hex" | |
) | |
func main() { | |
//u, _ := uuid.NewV4() | |
//fmt.Println("original uuid: "+u.String()) | |
u := "1d5c15dd-8e3d-4212-85ff-319dbd5d07e2" | |
encodeToHex(u) | |
encodeToBase64(u) | |
input := "aDxf5RxmTxyFatLyn1sNTA==" | |
decodeFromBase64(input) | |
ns := "clJYUtekQZy575dtofLrEA==" | |
decodeFromBase64(ns) | |
} | |
func encodeToHex(uuidString string) { | |
u, err := uuid.FromString(uuidString) | |
if err != nil { | |
panic(err) | |
} | |
fmt.Println("hex string => "+hex.EncodeToString(u.Bytes())) | |
} | |
// encode from uuid to base64 string | |
func encodeToBase64(uuidString string) { | |
u, err := uuid.FromString(uuidString) | |
if err != nil { | |
panic(err) | |
} | |
encoded := base64.StdEncoding.EncodeToString(u.Bytes()) | |
fmt.Println("encoded uuid => base64 string: "+encoded) | |
} | |
// decode from base64 string to uuid string | |
func decodeFromBase64(input string) { | |
decoded, err := base64.StdEncoding.DecodeString(input) | |
if err != nil { | |
panic(err) | |
} | |
uuid, err := uuid.FromBytes(decoded) | |
if err != nil { | |
panic(err) | |
} | |
fmt.Println("decoded base64 => uuid string: "+uuid.String()) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment