> 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/69-command-line-interface.md).

# Command-Line Arguments

This Go program demonstrates how to work with command-line arguments using the `os.Args` variable. Let's go through the code with inline comments:

```go
package main

import (
	"fmt"
	"os"
)

func main() {
	// os.Args provides a slice of command-line arguments, including the program name.
	argsWithProg := os.Args
	// os.Args[1:] gives a slice of command-line arguments excluding the program name.
	argsWithoutProg := os.Args[1:]

	// Accessing a specific command-line argument by index.
	arg := os.Args[3]

	// Print the command-line arguments.
	fmt.Println("Args with program name:", argsWithProg)
	fmt.Println("Args without program name:", argsWithoutProg)
	fmt.Println("Third argument (index 3):", arg)
}
```

Explanation:

1. **`os.Args`:**
   * `os.Args` is a slice of strings that represents the command-line arguments. The first element (`os.Args[0]`) is the program name.
2. **`argsWithProg`:**
   * `argsWithProg` contains all the command-line arguments, including the program name.
3. **`argsWithoutProg`:**
   * `argsWithoutProg` contains only the command-line arguments (excluding the program name).
4. **`arg`:**
   * `arg` is an example of accessing a specific command-line argument by index (in this case, the third argument at index 3).
5. **Printing:**
   * The program prints the command-line arguments to the console for demonstration.

When you run this program from the command line, for example:

```bash
go run main.go arg1 arg2 arg3
```

The output will be:

```
Args with program name: [./main arg1 arg2 arg3]
Args without program name: [arg1 arg2 arg3]
Third argument (index 3): arg3
```

Keep in mind that indexing starts at 0, so `os.Args[0]` is the program name.


---

# 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/69-command-line-interface.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.
