> For the complete documentation index, see [llms.txt](https://ineelhere.gitbook.io/code-with-go/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ineelhere.gitbook.io/code-with-go/60-base64-encoding.md).

# 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:

```go
// 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.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://ineelhere.gitbook.io/code-with-go/60-base64-encoding.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
