Golang flag.VisitAll() function example
package flag
VisitAll visits the command-line flags in lexicographical order, calling fn for each. It visits all flags, even those not set.
Golang flag.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
See https://www.socketloop.com/references/golang-flag-visit-function-example for comparison
Reference :
See also : Golang flag.Visit() function example
Advertisement
Something interesting
Tutorials
+3.7k Golang : Switch Redis database redis.NewClient
+7.1k Golang : Validate credit card example
+6.2k Golang : Process non-XML/JSON formatted ASCII text file example
+5.7k Golang : Error handling methods
+5.5k Golang : If else example and common mistake
+12.3k Golang : Display list of countries and ISO codes
+9.5k Golang : Accessing content anonymously with Tor
+21.2k Golang : Convert(cast) string to rune and back to string example
+12.3k Golang : 2 dimensional array example
+5.6k Golang : Configure crontab to poll every two minutes 8am to 6pm Monday to Friday
+7.5k Gogland : Single File versus Go Application Run Configurations
+36.4k Golang : Convert date or time stamp from string to time.Time type