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 OverflowGo 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
Go Packages
pkg.go.dev › encoding › base64
base64 package - encoding/base64 - Go Packages
September 1, 2026 - AppendEncode appends the base64 encoded src to dst and returns the extended buffer. func (enc *Encoding) Decode(dst, src []byte) (n int, err error)
Go auto decodes base64 encoded string while unmarshlling
The main thing is that for the ... and golang will just assume you want it to be converted from base64. If you have the target field as string it will just save it as is ... Maybe it's using a custom datatype that implements the Marshaller/Unmarshaller interfaces. ... Because the documentation says so. ... Yes. change my mind ... Super Micro-SaaS. Let's Talk Base64: What's Your Go-To Decoding ... More on reddit.com
More efficient image processing
This is trivially parallelizable using goroutines. See how kitty's own icat utility which is written in Go does it. In particular the Paste() and Parallel() functions in tools/utils/images More on reddit.com
I noticed ChatGPT can decode Base64. So I asked it how it's able to do that in Base64 (otherwise it denies in has this ability). This is the response. Is it bullshit?
Hey u/jsideris , if your post is a ChatGPT conversation screenshot, please reply with the conversation link or prompt. Thanks! We have a public discord server . There's a free Chatgpt bot, Open Assistant bot (Open-source model), AI image generator bot, Perplexity AI bot, 🤖 GPT-4 bot ( Now with Visual capabilities (cloud vision)! ) and channel for latest prompts! New Addition: Adobe Firefly bot and Eleven Labs cloning bot! So why not join us? NEW: Spend 20 minutes building an AI presentation | $1,000 weekly prize pool PSA: For any Chatgpt-related issues email support@openai.com I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
Is there a potential security issue if I receive a base64 string in my Express query?
Why do all this nonstandard stuff with base64 in the first place? Why don't you just use queryParams? That'll make your API less obtuse. And if the answer why you can't do that is that the query params would contain sensitive information... then base64 wouldn't save you there either. More on reddit.com
Top answer 1 of 5
69
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.
2 of 5
35
DecodedLen returns the maximal length.
This length is useful for sizing your buffer but part of the buffer won't be written and thus won't be valid UTF-8.
You have to use only the real written length returned by the Decode function.
l, _ := base64.StdEncoding.Decode(base64Text, []byte(message))
log.Printf("base64: %s\n", base64Text[:l])
ZetCode
zetcode.com › golang › base64
Base64 Encoding and Decoding in Go
July 16, 2026 - We encode the string <<Hello>> with both standard and URL-safe encoders. The < and > characters produce bytes that map to + and / in standard Base64, which are replaced with - and _ in URL-safe encoding. $ go run main.go StdEncoding: PDxIZWxsbz4+ URLEncoding: PDxIZWxsbz4- Decoded: <<Hello>>
TutorialsPoint
tutorialspoint.com › base64-decodestring-function-in-golang-with-examples
Base64 Package in Golang
package main import ( "encoding/base64" ... world! In this example, we define a base64 encoded string and use the DecodeString function from the base64 package to decode the data back into a byte slice....
GeeksforGeeks
geeksforgeeks.org › go language › base64-decodestring-function-in-golang-with-examples
base64.DecodeString() Function in Golang With Examples - GeeksforGeeks
May 19, 2020 - Return Value: It returns the bytes represented by the given base64 string. Below programs illustrate the DecodeString() function: Example 1: ... // Golang program to illustrate // the base64.DecodeString() Function package main import ( "encoding/base64" "fmt" ) func main() { // taking a string givenString := "R2Vla3Nmb3JHZWVrcw==" // using the function decodedString, err := base64.StdEncoding.DecodeString(givenString) if err != nil { fmt.Println("Error Found:", err) return } fmt.Print("Decoded Bytes: ") fmt.Println(decodedString) fmt.Print("Decoded String: ") fmt.Println(string(decodedString)) } Output:
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.
Golang Docs
golangdocs.com › home › base64 encoding and decoding in golang
Base64 Encoding and Decoding in Golang - Golang Docs
July 29, 2022 - The process is the same and uses the standard decoder from the package to decode the string. package main import ( "encoding/base64" "fmt" ) func main() { s := "a string" se := base64.StdEncoding.EncodeToString([]byte(s)) fmt.Println(se) // YSBzdHJpbmc= sd, e := base64.StdEncoding.DecodeString(se) if e != nil { fmt.Println(e) } fmt.Println(string(sd)) // a string }
Golang.cafe
golang.cafe › blog › golang-base64-encode-example
Golang base64 Encode Example | Golang.cafe
April 24, 2022 - // The encoder we will use is the base64.StdEncoding // It requires a byte slice so we cast the string to []byte encodedStr := base64.StdEncoding.EncodeToString([]byte(plainStr)) fmt.Println(encodedStr) // Decoding may return an error, in case the input is not well formed decodedStrAsByteSlice, err := base64.StdEncoding.DecodeString(encodedStr) if err != nil { panic("malformed input") } fmt.Println(string(decodedStrAsByteSlice)) }
GitHub
github.com › golang › go › blob › master › src › encoding › base64 › base64.go
go/src/encoding/base64/base64.go at master · golang/go
In this mode, the decoder requires that · // trailing padding bits are zero, as described in RFC 4648 section 3.5. // // Note that the input is still malleable, as new line characters · // (CR and LF) are still ignored. func (enc Encoding) Strict() *Encoding { enc.strict = true · return &enc · } · // StdEncoding is the standard base64 encoding, as defined in RFC 4648.
Author: golang
Go
go.dev › src › encoding › base64 › example_test.go
example_test.go
6 7 package base64_test 8 9 import ( 10 "encoding/base64" 11 "fmt" 12 "os" 13 ) 14 15 func Example() { 16 msg := "Hello, 世界" 17 encoded := base64.StdEncoding.EncodeToString([]byte(msg)) 18 fmt.Println(encoded) 19 decoded, err := base64.StdEncoding.DecodeString(encoded) 20 if err != nil { 21 fmt.Println("decode error:", err) 22 return 23 } 24 fmt.Println(string(decoded)) 25 // Output: 26 // SGVsbG8sIOS4lueVjA== 27 // Hello, 世界 28 } 29 30 func ExampleEncoding_EncodeToString() { 31 data := []byte("any + old & data") 32 str := base64.StdEncoding.EncodeToString(data) 33 fmt.Println(str) 34
Base64 Decode and Encode
base64decode.net › golang-decodestring
Golang DecodeString() - Base64 Decode
package main import b64 "encoding/base64" func main() { data := "YmFzZTY0IGVuY29kZWQgc3RyaW5n" sDec := b64.StdEncoding.DecodeString(data) } github.com/base64decoder/examples/blob/master/base64decode.go
Kelche
kelche.co › blog › go › golang-base64
Golang Base64 Encoding and Decoding - Kelche
January 24, 2023 - The json package provides the Marshal and Unmarshal functions for encoding and decoding JSON. The json package also provides the MarshalIndent function for encoding JSON with indentation. ... package main import ( "encoding/base64" "encoding/json" "fmt" ) type Person struct { Name string Age int } func main(){ p := Person{ Name: "John Doe", Age: 30, } data, err := json.MarshalIndent(p, "", " ") if err != nil { fmt.Println(err) } encoded := base64.StdEncoding.EncodeToString(data) fmt.Println(encoded) decoded, err := base64.StdEncoding.DecodeString(encoded) if err != nil { fmt.Println(err) } var p2 Person err = json.Unmarshal(decoded, &p2) if err != nil { fmt.Println(err) } fmt.Println(p2) }
Kirandev
kirandev.com › encode-decode-string-golang
How to Base64 encode and decode data in Golang
March 3, 2022 - EncodeToString encodes the passed data in Base64 format and returns the encoded data as a string. func (enc *Encoding) EncodeToString(src []byte) string · DecodeString decodes the passed data (string) and returns the decoded data as a byte slice.
Gomplate
docs.gomplate.ca › functions › base64
base64 functions - gomplate documentation
$ gomplate -i '{{ base64.DecodeBytes "aGVsbG8gd29ybGQ=" }}' [104 101 108 108 111 32 119 111 114 108 100]
7-Zip Documentation
documentation.help › golang › base64 - the go programming language
base64 - The Go Programming Language | Golang Documentation
The most common encoding is the "base64" encoding defined in RFC 4648 and used in MIME (RFC 2045) and PEM (RFC 1421). RFC 4648 also defines an alternate encoding, which is the standard encoding with - and _ substituted for + and /. ... NewEncoding returns a new Encoding defined by the given alphabet, which must be a 64-byte string. func (enc *Encoding) Decode(dst, src []byte) (n int, err error)