Base64 Encoding
This Go code demonstrates how to use the encoding/base64
package to encode and decode data using both standard and URL encoding. Let's go through the code with inline comments and explanations:
Output
Explanation:
Standard Base64 Encoding/Decoding:
b64.StdEncoding.EncodeToString([]byte(data))
encodes the data using standard base64 encoding.b64.StdEncoding.DecodeString(sEnc)
decodes the base64-encoded string back to the original data.
URL-Compatible Base64 Encoding/Decoding:
b64.URLEncoding.EncodeToString([]byte(data))
encodes the data using URL-compatible base64 encoding.b64.URLEncoding.DecodeString(uEnc)
decodes the URL-encoded base64 string back to the original data.
Both standard and URL-compatible base64 encodings are commonly used for encoding binary data in a way that can be safely transmitted over text-based protocols, such as HTTP, or embedded in data formats that do not handle binary data well. URL-compatible encoding is often used when the encoded data is part of a URL or needs to be included in a query parameter.
Last updated
Was this helpful?