Golang flag.FlagSet.NFlag() and Set() function example
package flag
NFlag returns the number of flags that have been set.
Set sets the value of the named flag.
Golang flag.FlagSet.NFlag() and Set() function usage example
package main
import (
"flag"
"fmt"
)
var flagSet *flag.FlagSet
func init() {
flagSet = flag.NewFlagSet("example", flag.ExitOnError)
}
func main() {
flagSet.String("file", "", "filename to view")
flagSet.Int("timeout", 5000, "specify the time in milliseconds to wait for a response")
flagSet.Parse([]string{"file","timeout"})
fmt.Println("Parsed the flag ? :", flagSet.Parsed())
// parsing the flags doesn't mean the NFlag count will increase
// will produce 0
fmt.Println("BEFORE : Number of flags set :", flagSet.NFlag())
// set file flag with a value, this will increase the NFlag value by 1
flagSet.Set("file", "textfile.txt")
// and by another 1
flagSet.Set("timeout", "5000")
flagNum := flagSet.NFlag()
fmt.Println("AFTER : Number of flags set : ", flagNum)
}
Output :
Parsed the flag ? : true
BEFORE : Number of flags set : 0
AFTER : Number of flags set : 2
References :
Advertisement
Something interesting
Tutorials
+6.7k Golang : Reverse by word
+9.1k Golang : Get curl -I or head data from URL example
+27.7k PHP : Count number of JSON items/objects
+13.9k Golang : convert(cast) string to float value
+10.3k Golang : How to check if a website is served via HTTPS
+11.8k Golang : Verify Linux user password again before executing a program example
+13.1k Golang : List objects in AWS S3 bucket
+13.5k Golang : Read XML elements data with xml.CharData example
+17.9k Golang : Login and logout a user after password verification and redirect example
+4.6k Mac OSX : Get disk partitions' size, type and name
+10.2k Golang : How to get quoted string into another string?
+10.8k Android Studio : Checkbox for user to select options example