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
+33.6k Golang : How to check if slice or array is empty?
+16.4k Golang : Send email and SMTP configuration example
+6.2k Golang : Get Hokkien(福建话)/Min-nan(閩南語) Pronounciations
+8.9k Golang : What is the default port number for connecting to MySQL/MariaDB database ?
+8.1k Golang : HTTP Server Example
+17.5k Golang : Linked list example
+7.5k Golang : How to handle file size larger than available memory panic issue
+17.1k Golang : XML to JSON example
+8.9k Golang : GMail API create and send draft with simple upload attachment example
+14.6k Golang : How to get URL port?
+10.4k Golang : cannot assign type int to value (type uint8) in range error
+26.3k Golang : Calculate future date with time.Add() function