> For the complete documentation index, see [llms.txt](https://go.netdpi.net/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://go.netdpi.net/13-the-core-packages/pkg_argv.md).

# 13.08-解析命令列參數

當我們在終端機執行指令時，可能需要傳遞參數給指令，如下列的 `go` 指令所示：

```
go run myfile.go
```

run 與 myfile.go 都是參數，我們也可以將旗標(flag)傳遞給參數：

```
go run -v myfile.go
```

flag package 可以讓我們的程式解析參數與旗標，這裡有一個範例程式，可以產生一個 0 \~ 6 的數字，我們透過將旗標（-max=100）傳遞給程式，以更改最大值（max value）：

```go
package main

import ("fmt";"flag";"math/rand")

func main() {
    // Define flags
    maxp := flag.Int("max", 6, "the max value")
    // Parse
    flag.Parse()
    // Generate a number between 0 and max
    fmt.Println(rand.Intn(*maxp))
}
```

不屬於旗標的那些額外參數可以使用 `flag.Args()` 進行解析，而這個函式會傳回一個 `[]string`。


---

# 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://go.netdpi.net/13-the-core-packages/pkg_argv.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.
