Standard library / flag

flag.FlagSet.Parse()

Parses registered flags; accepts an explicit argument slice and returns an error with ContinueOnError. Run with arguments: ["-x=5"]. Expected stdout: '5\n'.

Formal Syntax & Structure

go syntaxgo
func (f *FlagSet) Parse(arguments []string) error

Parameters & Attributes

NameTypeDescription
arguments[]stringArguments excluding the executable name.

Return Value

error

Parsing or setting error; nil on success.

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")  check(f.Parse(os.Args[1:]))  fmt.Println(*v)}
Go Reference