Base64 in Go

Published

In this guide

This developer guide on Base64Encoding explains go in plain language for developers and everyday users.

Everything important stays private: try the matching converter or tool in your browser — files and tokens are not uploaded.

Read the full English article below for code samples and edge cases, or switch language anytime from the menu. We keep expanding translations from real user feedback.

Try these private tools next

Go’s encoding/base64 package provides four ready-made encodings and lets you define custom ones.

Variable Alphabet Padding
base64.StdEncoding + / yes
base64.RawStdEncoding + / no
base64.URLEncoding - _ yes
base64.RawURLEncoding - _ no (JWT, URLs)

Strings

package main

import (
	"encoding/base64"
	"fmt"
)

func main() {
	s := base64.StdEncoding.EncodeToString([]byte("Grüße 👋"))
	fmt.Println(s) // R3LDvMOfZSDwn5GL

	b, err := base64.StdEncoding.DecodeString(s)
	if err != nil {
		panic(err) // illegal base64 data at input byte N
	}
	fmt.Println(string(b))
}

Go strings are UTF-8, so no conversion step is needed.

Decode errors

illegal base64 data at input byte 12 gives the exact position of the problem. Typical causes: Base64URL input decoded with StdEncoding, missing padding (use the Raw variant), or a data URI prefix. The decoder ignores \r and \n but nothing else.

Strict mode

enc := base64.StdEncoding.Strict() // rejects non-zero trailing bits

Without it, QQ== and QR== both decode to A. Strict mode matters when Base64 values are compared or used as keys.

Streaming

in, _ := os.Open("big.bin")
out, _ := os.Create("big.b64")
w := base64.NewEncoder(base64.StdEncoding, out)
io.Copy(w, in)
w.Close() // flushes the final partial block and padding

Forgetting Close() silently drops the last 1–2 bytes. Decode streams with base64.NewDecoder(base64.StdEncoding, r).

Appending without allocations (Go 1.22+)

buf = base64.StdEncoding.AppendEncode(buf, data)

Check your output with the Base64 decoder.

Usein kysytyt kysymykset

Does this page upload my data?

No. Articles are static. Linked tools run locally in your browser unless a feature explicitly needs a network relay.

Can I request a translation or a new tool?

Yes — use the private feedback form. We prioritize what users ask for across languages.

What is the fastest way to try go?

Open the homepage Base64 converter or the related tools linked on this page and paste a sample.

Aiheeseen liittyvät työkalut ja oppaat

Kommentit (0)

Kysymyksiä, ideoita tai bugi työkalussa “Base64 in Go”? Jätä kommentti, arvioi työkalu tai vastaa muille.

Ei vielä kommentteja. Ole ensimmäinen, joka jakaa palautteesi!

Jätä kommentti

Arvioi tämä työkalu:
Esc
↑ ↓ siirrä↵ avaaEsc sulje