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 OverflowThe 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.
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])
Encode []byte as base64 string (to be consistent with stdlib JSON)
Explain why []byte is base64 encoded when marshalled to json?
Base64 is the most widely used text representation of binary data.
More on reddit.comConvert data to base64 encode in go - Stack Overflow
Is it a good idea to convert images into base64 string and save them in database?
This is documented in the json package:
โ... []byte encodes as a base64-encoded string ...โ
Iโm curious about the motivation behind this design? Do other languages do this?
I understand that base64 encoding is done to transport binary data through transport mechanisms that are better at handling text. I guess you wouldnโt want a json array because it would be less efficient to marshal/unmarshal vs a string.
Base64 is the most widely used text representation of binary data.
It's because []byte != string if you've come from C or similar and have been using byte as a char replacement. You need some way to safely transport bytes since JSON values can only be string (UTF-8), number, object, array, bool & null.
I don't know about other languages but if you use Array#pack in Ruby to build a 'binary string' then JSON#generate will raise an Encoding::UndefinedConversionError exception if your 'binary string' can't be converted to UTF-8. So in Ruby you need to encode binary data (with base64, an array of integers to use with unpack or whatever) yourself to safely use it in JSON.
I am not sure what exactly is not clear from the documentation. Not only it has a clear name which explains states what the method is doing, it also has an example.
package main
import (
"encoding/base64"
"fmt"
)
func main() {
data := []byte("Pop")
str := base64.StdEncoding.EncodeToString(data)
fmt.Println(str) // UG9w
}
Go Playground
You can customise the output of print functions by providing a String() method for your type. Either for the whole Genre or just for the name variable.
Example:
package main
import (
"encoding/base64"
"fmt"
)
type Base64String string
func (b Base64String) String() string {
return base64.StdEncoding.EncodeToString([]byte(b))
}
type Genre struct {
Name Base64String
}
func main() {
a := []Genre{Genre{"Pop"}, Genre{"Rock"}}
fmt.Println(a) // prints [{UG9w} {Um9jaw==}]
fmt.Println(string(a[0].Name)) // prints Pop
}