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.9k Golang : Decode XML data from RSS feed
+7.8k Golang : Reverse a string with unicode
+14.6k Golang : Missing Bazaar command
+16.9k Golang : Get the IPv4 and IPv6 addresses for a specific network interface
+3.7k Golang : Switch Redis database redis.NewClient
+7.7k Golang : Test if an input is an Armstrong number example
+9.4k Golang : Find the length of big.Int variable example
+5.2k Golang : Issue HTTP commands to server and port example
+20k Golang : How to run your code only once with sync.Once object
+9k Golang : Get SPF and DMARC from email headers to fight spam
+6.9k Android Studio : Hello World example
+10.1k Golang : Compare files modify date example