Golang flag.Visit() function example
package flag
Visit visits the command-line flags in lexicographical order(also known as dictionary order or alphabetical order), calling fn (1st parameter) for each. It visits only those flags that have been set.
Golang flag.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 :
See also : Golang flag.VisitAll() function example
Advertisement
Something interesting
Tutorials
+29.5k Golang : Saving(serializing) and reading file with GOB
+6k Golang : Convert Chinese UTF8 characters to Pin Yin
+7.2k CloudFlare : Another way to get visitor's real IP address
+13k Golang : Calculate elapsed years or months since a date
+5.3k Golang : How to deal with configuration data?
+11.2k Golang : Fix - does not implement sort.Interface (missing Len method)
+5.7k List of Golang XML tutorials
+17.5k Golang : Find smallest number in array
+7.4k Golang : Example of custom handler for Gorilla's Path usage.
+38.1k Golang : Read a text file and replace certain words
+5.4k How to check with curl if my website or the asset is gzipped ?