Golang flag.FlagSet.VisitAll() function example
package flag
VisitAll visits the flags in lexicographical order, calling fn for each. It visits all flags, even those not set.
Golang flag.FlagSet.VisitAll() function usage example
package main
import (
"flag"
"fmt"
)
func main() {
f := flag.NewFlagSet("flag", flag.ExitOnError)
f.Int("int", 0, "int flag with value")
f.Int("int2", 0, "second 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)
fmt.Println("Visit All")
f.VisitAll(visitor) // this will display the int2 flag
}
Output :
First visit
Second visit
> int value= 108
Visit All
int value= 108
int2 value= 0
Reference :
Advertisement
Something interesting
Tutorials
+6.8k Golang : Muxing with Martini example
+6.7k Golang : Output or print out JSON stream/encoded data
+8.3k Golang: Prevent over writing file with md5 hash
+10.4k Golang : Simple Jawi(Yawi) to Rumi(Latin/Romanize) converter
+8.1k Golang : Append and add item in slice
+11.3k Golang : Characters limiter example
+6.3k Apt-get to install and uninstall Golang
+4.5k Java : Generate multiplication table example
+17k Golang : How to save log messages to file?
+23.1k Golang : simulate tail -f or read last line from log file example
+20k Golang : Convert(cast) bytes.Buffer or bytes.NewBuffer type to io.Reader
+10.7k Golang : Get currencies exchange rates example