To base64-decode a string in Go, you can use the decoding.DecodeString
function from the encoding/base64
package. This function takes a base64-encoded string as input and returns the decoded data as a byte slice.
Here’s an example:
package main
import (
"encoding/base64"
"fmt"
)
func main() {
// The string to be decoded:
encoded := "SGVsbG8gV29ybGQh"
// Decode the string:
decoded, err := base64.StdEncoding.DecodeString(encoded)
if err != nil {
fmt.Println("Error while decoding the string",
err.Error())
}
// Print the decoded data:
fmt.Println(string(decoded))
// Output: Hello World!
// Encode it back:
enc := base64.StdEncoding.EncodeToString(decoded)
// Output: SGVsbG8gV29ybGQh
fmt.Println("encoded", enc)
}
That was your nugget 😄.
Until the next one… May the source be with you 🦄.