Code with Go
About
  • Code With Go! 🚀
  • Hello World
  • Values
  • Variables
  • Constants
  • For Loop
  • If Else
  • Switch
  • Array
  • Slice
  • Map
  • Range
  • Functions
  • Multiple Return Values
  • Variadic Functions
  • Closure
  • Recursion
  • Pointers
  • Strings and Runes
  • Structs
  • Methods
  • Interfaces
  • Struct Embedding
  • Generics
  • Error
  • Goroutines
  • Channels
  • Channel Buffering
  • Channel Synchronization
  • Channel Directions
  • Select
  • Timeouts
  • Non-Blocking Channel Operations
  • Closing Channels
  • Range over Channels
  • Timers
  • Tickers
  • Worker Pools
  • Wait Groups
  • Rate Limiting
  • Atomic Counters
  • Mutexes
  • Stateful Goroutines
  • Sorting
  • Sorting by Functions
  • Panic
  • Defer
  • Recover
  • String Functions
  • String Formatting
  • Text Templates
  • Regular Expressions
  • JSON
  • XML
  • Time
  • Epoch
  • Time Formatting / Parsing
  • Random Numbers
  • Number Parsing
  • SHA256 Hashes
  • Base64 Encoding
  • Reading Files
  • Writing Files
  • Line Filters
  • File Paths
  • Directories
  • Temporary Files and Directories
  • Embed Directive
  • Testing and Benchmarking
  • Command-Line Arguments
  • Command-Line Flags
  • Command-Line Subcommands
  • Environment Variables
  • Logging
  • HTTP Client
  • HTTP Server
  • Context
  • Spawning Processes
  • Exec'ing Processes
  • Signals
  • Exit
  • Resize Image
Powered by GitBook
On this page

Was this helpful?

Edit on GitHub

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.

PreviousSHA256 HashesNextReading Files

Last updated 1 year ago

Was this helpful?