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.2k Golang : Get Hokkien(福建话)/Min-nan(閩南語) Pronounciations
+11.1k Golang : Read until certain character to break for loop
+10.4k Golang : Generate random integer or float number
+13k Golang : Get terminal width and height example
+6.3k Golang : Test input string for unicode example
+4.4k Linux/MacOSX : Search and delete files by extension
+8.9k Golang : What is the default port number for connecting to MySQL/MariaDB database ?
+9k Golang : Capture text return from exec function example
+6k Golang : Convert Chinese UTF8 characters to Pin Yin
+7.4k Golang : Check to see if *File is a file or directory
+16.3k Golang : Find out mime type from bytes in buffer
+11.3k Golang : Post data with url.Values{}