Skip to main content

How to Decode Base64 in Go

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

Base64 turns bytes into plain text so they can travel in JSON, URLs or environment variables. Go's encoding/base64 package turns that text back into bytes with DecodeString, and returns an error when the text isn't valid base64.

Syntax​

Syntax
import "encoding/base64"

<data>, err := base64.StdEncoding.DecodeString(<text>)
PartWhat it does
base64.StdEncodingStandard base64, with +, / and = padding.
.DecodeString(<text>)Decodes the text and returns a []byte and an error.
errnil on success; otherwise it says where the input went wrong.

The encoding/base64 documentation: "DecodeString returns the bytes represented by the base64 string s. If the input is malformed, it returns the partially decoded data and CorruptInputError."

The Example​

The example decodes SGVsbG8=, the base64 form of the word Hello, prints the bytes and then the text.

Prerequisites​

  • Go. go run compiles and runs a single file. This guide uses Go 1.27.4.

Step 1: Write the Code​

Create a file named main.go:

main.go, which decodes SGVsbG8= with base64.StdEncoding.DecodeString, prints Not valid base64: and returns on an error, and otherwise prints the bytes and string(data).

main.go
package main

import (
"encoding/base64"
"fmt"
)

func main() {
data, err := base64.StdEncoding.DecodeString("SGVsbG8=")
if err != nil {
fmt.Println("Not valid base64:", err)
return
}
fmt.Println(data)
fmt.Println(string(data))
}

string(data) converts the bytes to a string, which shows the text they hold.

Step 2: Run It​

Terminal
go run main.go
Output
[72 101 108 108 111]
Hello

A zsh terminal where go run main.go prints [72 101 108 108 111] and Hello.

The five numbers are the bytes of H, e, l, l and o. The command is the same in zsh, bash and PowerShell.

Base64url and the Way Back​

Base64url, used in JWTs and URLs, writes - and _ and usually leaves out the = padding. Decode it with base64.RawURLEncoding.DecodeString, which returned the bytes [251 255] for -_8. To encode, use base64.StdEncoding.EncodeToString([]byte("Hello")), which returned SGVsbG8=.

Common Mistakes​

MistakeWhat happensDo this instead
Decoding base64url text with StdEncodingillegal base64 data at input byte 0 for -_8Use base64.RawURLEncoding
Leaving out the = padding with StdEncodingillegal base64 data at input byte 4 for SGVsbG8Keep the padding, or use RawStdEncoding
Ignoring the errorA partial result is used as if it were completeCheck err

Troubleshooting​

SymptomLikely causeFix
illegal base64 data at input byte NA character the encoding doesn't allow, or missing padding, at position NPick the encoding that matches the input
Garbled text after string(data)The bytes aren't text, such as a keyKeep them as []byte

FAQ​

Which encodings does the package have?​

Four: StdEncoding and URLEncoding with padding, and RawStdEncoding and RawURLEncoding without.

Does DecodeString accept new lines?​

Yes. The documentation says new line characters are ignored.

Conclusion​

base64.StdEncoding.DecodeString(text) returns the decoded bytes and an error; check the error, then use string(data) when the bytes are text. Use RawURLEncoding for base64url.

Additional Resources​