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:

// Importing necessary packages.
import (
	b64 "encoding/base64"
	"fmt"
)

// The main function, where the execution of the program begins.
func main() {
	// The data to be encoded.
	data := "abc123!?$*&()'-=@~"

	// Standard Encoding

	// Encoding the data using standard base64 encoding.
	sEnc := b64.StdEncoding.EncodeToString([]byte(data))
	fmt.Println(sEnc)

	// Decoding the base64-encoded string back to the original data.
	sDec, _ := b64.StdEncoding.DecodeString(sEnc)
	fmt.Println(string(sDec))
	fmt.Println()

	// URL Encoding

	// Encoding the data using URL-compatible base64 encoding.
	uEnc := b64.URLEncoding.EncodeToString([]byte(data))
	fmt.Println(uEnc)

	// Decoding the URL-encoded base64 string back to the original data.
	uDec, _ := b64.URLEncoding.DecodeString(uEnc)
	fmt.Println(string(uDec))
}

Output

YWJjMTIzIT8kKiYoKSctPUB+
abc123!?$*&()'-=@~

YWJjMTIzIT8kKiYoKSctPUB-
abc123!?$*&()'-=@~

Explanation:

  1. 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.

  2. 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