👈 Decode Base64 to a string in Go

base64 encoding strings

To decode a Base64 encoded string, use the DecodeString() function from the encoding/base64 standard library package. This function takes an encoded string as an argument and returns a byte slice, along with the decoding error. To get the decoded string, convert the byte slice to a string.

Check also how to encode a string to Base64 in Go.

package main

import (
    "encoding/base64"
    "fmt"
)

func main() {
    rawDecodedText, err := base64.StdEncoding.DecodeString("aGVsbG8gZnJvbSBnb3NhbXBsZXMuZGV2IGJhc2U2NCBlbmNvZGluZyBleGFtcGxlIQ==")
    if err != nil {
        panic(err)
    }
    fmt.Printf("Decoded text: %s\n", rawDecodedText)
}

Output:

Decoded text: hello from gosamples.dev base64 encoding example!

The StdEncoding is an object representing the standard base64 encoding, as defined in RFC 4648, and the DecodeString() is its method.

👉 Encode a string to Base64 in Go

Learn how to encode strings to Base64 using encoding/base64 package
base64 encoding strings

🖨️ Convert string to []byte or []byte to string in Go

Learn the difference between a string and a byte slice
introduction strings slice

🔟 Convert string to bool in Go

Learn how to parse a string as a bool
introduction strings bool