Golang flag.Visit() function example

package flag

Visit visits the command-line flags in lexicographical order(also known as dictionary order or alphabetical order), calling fn (1st parameter) for each. It visits only those flags that have been set.

Golang flag.Visit() function usage example

 package main

 import (
 "flag"
 "fmt"
 )



 func main() {
 f := flag.NewFlagSet("flag", flag.ExitOnError)
 f.Int("int", 0, "int flag with value")

 visitor := func(a *flag.Flag) {
 fmt.Println(">", a.Name, "value=", a.Value)
 }


 fmt.Println("First visit")
 f.Visit(visitor) // no value assigned

 f.Parse([]string{"-int", "108"}) // visit flag set earlier and assign value

 fmt.Println("Second visit")
 f.Visit(visitor)

 }

Output :

First visit

Second visit

> int value= 108

Reference :

http://golang.org/pkg/flag/#Visit

  See also : Golang flag.VisitAll() function example

Advertisement