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 - StdEncoding is the standard base64 encoding, as defined in RFC 4648.
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
Returning png images and data from an api
Why would you pass back the image? Vs providing a url where the caller can go get it from? Ultimately, you would need to change the mime type, and pass the image as a []byte if memory serves, so I don't think you'll be able to do it as the same API request? Without encoding it More on reddit.com
When is base64 relevant to me? Is this something that works under the hood?
Most HTTP connections deal with sending text back and forth. If you need to send binary data, you can first encode it in base64 to ensure that the entire thing is only represented using alphanumeric (and a few symbols). Then on the other end it can be decoded back into binary. More on reddit.com
Is it a good idea to convert images into base64 string and save them in database?
You will get many people saying don't because if not implemented well it can bite ya. Don't be concerned about "optimal", tell yourself "good enough" and "don't let perfect be the enemy of good". But I have done this for several projects (enterprise, mission critical systems) and it works just fine. From the developer standpoint I love it. When I yank the "byte array" from the DB I can just pass it right to the client as-is, just adding on the mime type, super simple. Right now I have a system that stores photos of students at a university and we serve those out with an API. All done in C# and SQL Server. We have probably 50,000 photos and it is lighting fast. That being said I have also stored files in S3 and just stored the name in the DB. I will usually rename the file so S3 stores it efficiently. I like things like s3 better than a file system since you can dedicate a bucket just to this and some sysadmin won't go mucking around. I agree with you on storing the unstructured binary data along with your structured data for simplicity. I had a project once that wrote images to a filesystem. Years later older images were lost due to some file system cleanup process. Over my 25 years programming I have found you cannot guess where the issues will be. Don't optimize until you measure it. If you can build a proof of concept then hammer it, do it. Write a script that inserts and reads images and loop it for a few hours. See what happens. Also if you implement something and a few years later it starts to falter, then refactor. But you need to monitor! This is another place where people get bit, they do something like this and just let it run in a corner until it "just dies for no reason" one day. You or someone needs to care and feed for it. I am going to assume on-prem Microsoft SQL Server, your usage may vary. Use "VARBINARY(MAX)" for the field type https://docs.microsoft.com/en-us/sql/t-sql/data-types/binary-and-varbinary-transact-sql?view=sql-server-ver15 Put it in a table by itself with an id to look it up. Store filename, and filetype(mime-type) too if needed. Slap an index on the id, reference that id in other tables. Critical: Put the table in its own filegroup separate from the other tables. This will allow for faster restores since you can restore the primary filegroup and get your DB back online ASAP, then as it is up and running restore the filegroup with the images. So instead of the whole DB being down for hours, it is up in minutes with only the images being unavailable for hours. https://docs.microsoft.com/en-us/sql/relational-databases/databases/database-files-and-filegroups?view=sql-server-ver15 As with all DBs the filegroups should not be on the system (C) drive, maybe even put the images table filegroup on its own drive with nothing else. Do set a limit on the incoming filesize in your code. Someone could send in a few Blu-ray ISOs and cripple your system. In .NET code you store the image data in a byte array "byte[] " variable. That byte array is what is stored in the varbinary(max) DB field. It is easy to convert the incoming image/file to byte[], just google around for that there are many easy to do it. Design and implement a cleanup process and purge old unneeded records. No one ever seems to do this and the data grows forever until the system is retired/migrated. For small DBs it's not a problem but for this you must go back to your stakeholder and get in writing when you can delete old records then set an automated process or scheduled manual progress to do it. If they need the data long term maybe older data can be moved out of the online system and to an archive or warehouse. Microsoft has some other methods that I have not used. Check those out. https://docs.microsoft.com/en-us/sql/relational-databases/blob/binary-large-object-blob-data-sql-server?view=sql-server-ver15 -- Good luck More on reddit.com
How to decode base64 data in golang?
Use base64.StdEncoding.DecodeString(s) which returns ([]byte, error); wrap with string() if you need text and validate errors from corrupt input.
golinuxcloud.com
golinuxcloud.com › home › programming › golang base64 encode and decode with encoding/base64 and json
Golang base64 encode & decode — base64 golang, JSON []byte
How do I golang json prevent encoding byte to base64?
Store the payload in a string field, use json.RawMessage for already-JSON bytes, or define a named type with MarshalJSON that writes the shape you need instead of the default []byte rule.
golinuxcloud.com
golinuxcloud.com › home › programming › golang base64 encode and decode with encoding/base64 and json
Golang base64 encode & decode — base64 golang, JSON []byte
Which base64 encoding should I use?
StdEncoding for a body or a file, URLEncoding when the
value goes into a URL or a filename, and the Raw variants when the recipient rejects = padding.
callicoder.com
callicoder.com › home › golang › base64 encoding and decoding in golang
Base64 Encoding and Decoding in Golang | CalliCoder
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 - This Go Base64 tutorial shows how to encode and decode binary data to and from Base64 in Go using the encoding/base64 package.
CalliCoder
callicoder.com › home › golang › base64 encoding and decoding in golang
Base64 Encoding and Decoding in Golang | CalliCoder
July 1, 2021 - 2021-07-01 · Golang · 12 min read · Four encodings in one package and why picking the wrong one breaks a URL, the padding that some APIs reject, and the streaming encoder that avoids holding the whole payload twice. encoding/base64 exposes four pre-built encodings rather than one, and the ...
Gomplate
docs.gomplate.ca › functions › base64
base64 functions - gomplate documentation
base64.Encode Encode data as a Base64 string. Specifically, this uses the standard Base64 encoding as defined in RFC4648 §4 (and not the URL-safe encoding). Added in gomplate v1.8.0 Usage base64.Encode input input | base64.Encode Arguments name description input (required) The data to encode.
Golang Docs
golangdocs.com › home › base64 encoding and decoding in golang
Base64 Encoding and Decoding in Golang - Golang Docs
July 29, 2022 - 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 } Recommended: AES Encryption in Golang ·
Go
go.dev › src › encoding › base64 › example_test.go
- The Go Programming Language
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
gosamples
gosamples.dev › tutorials › encode a string to base64 in go
👉 Encode a string to Base64 in Go
October 14, 2021 - To encode a string to Base64 in Go, use the EncodeToString() function from the encoding/base64 standard library package.
7-Zip Documentation
documentation.help › golang › base64 - the go programming language
base64 - The Go Programming Language | Golang Documentation
StdEncoding is the standard base64 encoding, as defined in RFC 4648.
Kirandev
kirandev.com › encode-decode-string-golang
How to Base64 encode and decode data in Golang
March 3, 2022 - Read more about the Base64 encoding scheme here. Golang provides built-in support for base64 encoding and decoding with a package encoding/base64.