Standard library / flag
flag.FlagSet.Arg()
Returns the indexed positional argument; missing indexes return an empty string. Run with arguments: ["file"]. Expected stdout: '"file" ""\n'.
Formal Syntax & Structure
go syntaxgo
func (f *FlagSet) Arg(i int) stringParameters & Attributes
| Name | Type | Description |
|---|---|---|
| i | int | Zero-based index among remaining positional arguments. |
Return Value
string
Returns the indexed positional argument; missing indexes return an empty string.
Working Example
Example Snippetgo
package main
import ( "flag" "fmt" "os")
func check(e error) { if e != nil { panic(e) }}
func main() { f := flag.NewFlagSet("demo", flag.ContinueOnError) v := f.Int("x", 0, "value") _ = v check(f.Parse(os.Args[1:])) fmt.Printf("%q %q\n", f.Arg(0), f.Arg(2))}