Decode and Encode Base64 in Go
Go supports base64 encoding/decoding out of the box with the built-in encoding/base64
package. See code snippets below.
Decoding Base64 string to text
package main
import (
"encoding/base64"
"fmt"
)
func main() {
base64text := "aHR0cHM6Ly9lYXN5NjQub3Jn"
decodedText, _ := base64.StdEncoding.DecodeString(base64text)
fmt.Println(decodedText)
}
Encoding Base64 string
package main
import (
"encoding/base64"
"fmt"
)
func main() {
textToEncode := "Go is cool"
base64text := base64.StdEncoding.EncodeToString([]byte(textToEncode))
fmt.Println(base64text)
}