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
+7.1k Golang : Squaring elements in array
+14.3k Golang : Get uploaded file name or access uploaded files
+7.3k Golang : How to fix html/template : "somefile" is undefined error?
+13.3k Golang : Date and Time formatting
+13.9k Golang : How to check if a file is hidden?
+7.2k Golang : Dealing with postal or zip code example
+7.3k Golang : Not able to grep log.Println() output
+22.9k Golang : Test file read write permission example
+28k Golang : Move file to another directory
+5.4k Golang : Reclaim memory occupied by make() example
+10.3k Golang : Detect number of faces or vehicles in a photo
+29.9k Golang : How to get HTTP request header information?