Skip to main content
  1. Tutorials/

👈 Decode Base64 to a string in Go

·1 min

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.