# Environment Variables

This Go program demonstrates how to work with environment variables using the `os` package. It sets the value of the "FOO" environment variable, retrieves its value, and prints all environment variables. Let's break down the code with inline comments:

```go
package main

import (
	"fmt"
	"os"
	"strings"
)

func main() {
	// Set the value of the "FOO" environment variable to "1"
	os.Setenv("FOO", "1")

	// Retrieve and print the value of the "FOO" environment variable
	fmt.Println("FOO:", os.Getenv("FOO"))

	// Retrieve and print the value of the "BAR" environment variable (not set)
	fmt.Println("BAR:", os.Getenv("BAR"))

	fmt.Println()

	// Iterate through all environment variables and print their names
	for _, e := range os.Environ() {
		// Split the environment variable into a key-value pair
		pair := strings.SplitN(e, "=", 2)
		// Print the name of the environment variable
		fmt.Println(pair[0])
	}
}
```

Explanation:

1. **Setting Environment Variable:**
   * The program uses `os.Setenv` to set the value of the "FOO" environment variable to "1".
2. **Retrieving Environment Variable Value:**
   * It uses `os.Getenv` to retrieve and print the value of the "FOO" environment variable.
3. **Checking for Unset Environment Variable:**
   * It retrieves and prints the value of the "BAR" environment variable, which is not set. This will print an empty string.
4. **Printing All Environment Variables:**
   * The program uses `os.Environ` to retrieve all environment variables and then iterates through them.
   * For each environment variable, it splits the key-value pair and prints the name of the environment variable.

You can run the program to see the values of the "FOO" and "BAR" environment variables, as well as all other environment variables set in your system:

```bash
go run main.go
```


---

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

```
GET https://ineelhere.gitbook.io/code-with-go/72-environment-variables.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
