Go Base64 Encoding
Base64 encoding allows you to represent binary data (like images or encrypted files) as a plain text string. This is useful for sending data over protocols that only support text, like HTTP headers or email.
1. Basic Encoding and Decoding
2. URL-Safe Encoding
Standard Base64 contains characters like + and / that have special meanings in URLs. Use URLEncoding to swap them for - and _.
3. Encodings without Padding
By default, Base64 adds = at the end to make the string a specific length. You can remove this using the Raw variants.
Why use Base64?
- Transport Safe: Binary data can contain "invisible" control characters that break text-based protocols. Base64 is guaranteed safe.
- Standards: It's the standard for Basic Auth, JWT tokens, and embedding images in HTML.
- Data URLs: You can represent a whole file inside a single string:
data:image/png;base64,iVBORw....