Golang flag.FlagSet.Visit() function example
package flag
Visit visits the flags in lexicographical order, calling fn(1st parameter) for each. It visits only those flags that have been set.
Golang flag.FlagSet.Visit() function usage example
package main
import (
"flag"
"fmt"
)
func main() {
f := flag.NewFlagSet("flag", flag.ExitOnError)
f.Int("int", 0, "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)
}
Output :
First visit
Second visit
> int value= 108
Reference :
Advertisement
Something interesting
Tutorials
+4.3k Javascript : How to show different content with noscript?
+9.4k Golang : Find the length of big.Int variable example
+9k Golang : Get SPF and DMARC from email headers to fight spam
+6.3k Apt-get to install and uninstall Golang
+16.3k Golang : convert string or integer to big.Int type
+5.2k Golang : Print instead of building pyramids
+11.3k Golang : Fix fmt.Scanf() on Windows will scan input twice problem
+62.7k Golang : Convert HTTP Response body to string
+17.2k Golang : When to use init() function?
+6.9k Golang : Calculate BMI and risk category
+6k Golang : Convert Chinese UTF8 characters to Pin Yin
+20.7k Golang : Read directory content with os.Open