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
+26.9k Golang : Force your program to run with root permissions
+20.3k Swift : Convert (cast) Int to int32 or Uint32
+7.9k Setting $GOPATH environment variable for Unix/Linux and Windows
+13.6k Golang : Get user input until a command or receive a word to stop
+4.9k Javascript : How to get width and height of a div?
+5.8k Golang : List all packages and search for certain package
+5k Golang : micron to centimeter example
+4.3k Javascript : How to show different content with noscript?
+32.5k Golang : Copy directory - including sub-directories and files
+15.2k Golang : How to add color to string?
+25.7k Golang : missing Mercurial command
+6.9k Mac OSX : Find large files by size