The len prefix is superficial and causes the invalid utf-8 error:

package main

import (
        "encoding/base64"
        "fmt"
        "log"
)

func main() {
        str := base64.StdEncoding.EncodeToString([]byte("Hello, playground"))
        fmt.Println(str)

        data, err := base64.StdEncoding.DecodeString(str)
        if err != nil {
                log.Fatal("error:", err)
        }

        fmt.Printf("%q\n", data)
}

(Also here)


Output

SGVsbG8sIHBsYXlncm91bmQ=
"Hello, playground"

EDIT: I read too fast, the len was not used as a prefix. dystroy got it right.

Answer from zzzz on Stack Overflow
🌐
Go Packages
pkg.go.dev › encoding › base64
base64 package - encoding/base64 - Go Packages
September 1, 2026 - StdEncoding is the standard base64 encoding, as defined in RFC 4648.
🌐
Go by Example
gobyexample.com › base64-encoding
Go by Example: Base64 Encoding
Go provides built-in support for base64 encoding/decoding · This syntax imports the encoding/base64 package with the b64 name instead of the default base64. It’ll save us some space below
Discussions

go - Encode/Decode base64 - Stack Overflow
To sum up the other two posts, here are two simple functions to encode/decode Base64 strings with Go: More on stackoverflow.com
🌐 stackoverflow.com
Go auto decodes base64 encoded string while unmarshlling
Posted by u/Icy_Handle6544 - 0 votes and 12 comments More on reddit.com
🌐 r/golang
12
0
January 1, 2026
Doing base64 decoding on a string in Go - Stack Overflow
I have a particular string that I need to run base64 decode on in Go. This string looks something like this: qU4aaakFmjaaaaI5aaa\/EN\/aaa\/SaaaJaaa6aa+nGnk= Please note this is not the exact same s... More on stackoverflow.com
🌐 stackoverflow.com
google app engine - Base64 encode/decode without padding on golang ( appengine ) - Stack Overflow
There's a way to encode/decode a string to/from Base64 without having the padding at the end? I mean the '==' ending. I'm using base64.URLEncoding.EncodeToString to encode and it works perfectly b... More on stackoverflow.com
🌐 stackoverflow.com
🌐
GeeksforGeeks
geeksforgeeks.org › go language › base64-decodestring-function-in-golang-with-examples
base64.DecodeString() Function in Golang With Examples - GeeksforGeeks
May 19, 2020 - Go language provides inbuilt support for base64 encoding/decoding and has functions that could be used to perform operations on the given data using the base64 package. This package provides DecodeString() function which is used to decode a ...
🌐
GitHub
github.com › golang › go › blob › master › src › encoding › base64 › base64.go
go/src/encoding/base64/base64.go at master · golang/go
// Package base64 implements base64 encoding as specified by RFC 4648. package base64 · · import ( "internal/byteorder" "io" "math" "slices" "strconv" ) · /* * Encodings · */ · // An Encoding is a radix 64 encoding/decoding scheme, ...
Author: golang
🌐
Leapcell
leapcell.io › blog › working-with-base64-in-go
Working with Base64 in Go | Leapcell
July 25, 2025 - Base64 encoding is a common technique used to represent binary data in an ASCII string format. This is particularly useful when transmitting data over media that are designed to handle textual data.
Find elsewhere
🌐
GitHub
github.com › manifoldco › go-base64
GitHub - manifoldco/go-base64: A raw base64 url encoding library for JSON · GitHub
package main import ( "encoding/json" "fmt" "github.com/manifoldco/go-base64" ) type Example struct { Name string `json:"name"` Data *base64.Value `json:"data"` RegularData []byte `json:"regular"` } func main() { sample := []byte{0xF, 0xEE, 0xD, 0xC, 0x0D} e := &Example{ Name: "test data", Data: base64.New(sample), RegularData: sample, } o, _ := json.MarshalIndent(e, "", " ") fmt.Println(string(o)) }
Author: manifoldco
🌐
Reddit
reddit.com › r/golang › go auto decodes base64 encoded string while unmarshlling
r/golang on Reddit: Go auto decodes base64 encoded string while unmarshlling
January 1, 2026 - As abofh noted it’s normal. The main thing is that for the target struct in golang you just define it as byte[] and golang will just assume you want it to be converted from base64.
🌐
Medium
medium.com › @jcox250 › generating-prefixed-base64-ids-in-golang-e7731bd89c1b
Generating Prefixed Base64 ID’s In Golang | by James Cox | Medium
September 3, 2017 - // Generate a random base64 url encoded string func generateBase64ID(size int) (string, error) { // First create a slice of bytes b := make([]byte, size) // Read size number of bytes into b _, err := rand.Read(b) if err != nil { return "", err } // Encode our bytes as a base64 encoded string using URLEncoding encoded := base64.URLEncoding.EncodeToString(b) return encoded, nil }
🌐
Go
go.dev › src › encoding › base64 › base64.go
package base64 - encoding
Data written to 275 // the returned writer will be encoded using enc and then written to w. 276 // Base64 encodings operate in 4-byte blocks; when finished 277 // writing, the caller must Close the returned encoder to flush any 278 // partially written blocks.
🌐
ZetCode
zetcode.com › golang › base64
Base64 Encoding and Decoding in Go
July 16, 2026 - This Go Base64 tutorial shows how to encode and decode binary data to and from Base64 in Go using the encoding/base64 package.
🌐
Golang.cafe
golang.cafe › blog › golang-base64-encode-example
Golang base64 Encode Example | Golang.cafe
April 24, 2022 - Golang provides built in support for base64 encoding/decoding. Directly in the standard library we have “encoding/base64” so there is no need to download or install third-party libraries.
🌐
gosamples
gosamples.dev › tutorials › decode base64 to a string in go
👈 Decode Base64 to a string in Go
October 14, 2021 - 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.
🌐
Medium
wgallagher86.medium.com › hex-to-base64-encoding-in-go-ee7fd8e8fd69
Hex to Base64 Encoding in Go. An in depth look at Solving Cryptopals… | by William Gallagher | Medium
December 9, 2019 - Hex to Base64 Encoding in Go Intro I’ve recently started a new job as a security engineer, and in preparation I’ve been going through the Cryptopals Crytpo challenges. In the first challenge, we …
🌐
Google Groups
groups.google.com › g › golang-nuts › c › IN3X1Hg5IJY
Base64 Encoding
import ( "flag"; "encoding/base64"; "fmt"; "strings"; "bytes"; ) var username = flag.String("u", "anonymous", "Username"); var password = flag.String("p", "password", "Password"); func main() { flag.Parse(); fmt.Printf("HTTP Credentials = %s:%s\n", *username, *password); // base64 encode the username and password var src []byte = strings.Bytes(*username + ":" + *password); var dest []byte; var encoding = base64.NewEncoding ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"); encoding.Encode(dest, src); // convert bytes to string var destStr = bytes.NewBuffer(dest).String(); //fmt.Printf("Authorization: basic-%s", destStr); } The exception happens during encoding.Encode(dest, src) Any Ideas?
🌐
Gomplate
docs.gomplate.ca › functions › base64
base64 functions - gomplate documentation
This function outputs the data as a byte array, so it’s most useful for outputting binary data that will be processed further. Use base64.Decode to output a plain string.
🌐
Reddit
reddit.com › r/golang › base64 encoding question
r/golang on Reddit: Base64 encoding question
February 8, 2021 - You would have to serialize the struct into a byte stream first, using JSON, GOB, or something like that, then base64 encode that.
🌐
Educative
educative.io › answers › golang-base64-encoding-decoding
Golang base64 encoding/decoding
Golang provides built-in support for base64 encoding/decoding in the package encoding/base64.