A small but very efficient and fast base64 encoding/decoding library for go that offers a variety of default charsets but also the option to use your very own, user-defined charset.
An encoder is used for both, encoding and decoding. Every encoder you create is tied to its very own, immutable charset. This ensures consistency when encoding/decoding with the same encoder.
You may create an encoder in one of the following ways:
- encoder with the default charset that is url-safe but non-standard:
encoder := base64encoding.New()
- encoder with the standard base64 charset (e.g. used for encoding pictures in base64 in HTML) that is not url-safe:
encoder := base64encoding.NewWeb()
- encoder with a custom charset (or a constant of this library that wasn't given its own constructor) that is defined by the user.
Note that the charset must be comprised of exactly 64 pairwise distinct ASCII characters:
myCharset := base64encoding.EasilyReadableCodeSet // or myCharset := "my_custoM-CharSeT+0123456789..." (omitting the rest) encoder, err := base64encoding.NewCustom(myCharset) if err != nil { // handle error caused by illegal charset (e.g. not pairwise distinct) }
Once you have created your encoder you may use it like that:
data := []byte("some data: could be a picture, a UUID or whatever")
stringB64encoded := encoder.Encode(data)
data, err := encoder.Decode(stringB64encoded)
if err != nil {
// handle error (e.g. string not encoded with the encoder's charset)
}
- encoders are thread-safe. You may use the same encoder accross mutliple threads simultaneously, as the charset is read-only.