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
+8.5k PHP : How to parse ElasticSearch JSON ?
+11.6k Swift : Convert (cast) Float to String
+20.2k Golang : Reset or rewind io.Reader or io.Writer
+19.2k Golang : Delete item from slice based on index/key position
+8.3k Golang : Number guessing game with user input verification example
+13.9k Golang : Get current time
+12.5k Golang : Forwarding a local port to a remote server example
+4.7k Golang : How to pass data between controllers with JSON Web Token
+8.8k Golang : On lambda, anonymous, inline functions and function literals
+16.4k Golang : Test floating point numbers not-a-number and infinite example
+22.4k Golang : How to read JPG(JPEG), GIF and PNG files ?
+5.7k Fix yum-complete-transaction error