Cookies management by TermsFeed Cookie Consent
Russia has invaded Ukraine and already killed tens of thousands of civilians, with many more raped or tortured. It's a genocide. We need your help. Let's fight back against the Russian regime.
Help Ukraine! Fight the Russian regime!

👈 Decode Base64 to a string in Go

base64 encoding strings

Please consider supporting us by disabling your ad blocker

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

🧮 Sort a string slice containing numbers in Go

Learn how to sort strings with numbers in natural order
sort slice strings

🍒 Concatenate strings in Go

Learn the differences between string concatenation methods
introduction strings