Golang flag.FlagSet.Parse() and Parsed() functions example
package flag
Parse parses flag definitions from the argument list, which should not include the command name. Must be called after all flags in the FlagSet are defined and before flags are accessed by the program. The return value will be ErrHelp if -help was set but not defined.
Golang flag.FlagSet.Parse() function usage example
package main
import (
"flag"
"fmt"
)
var (
flagSet *flag.FlagSet
)
func init() {
flagSet = flag.NewFlagSet("example", flag.ContinueOnError)
}
func main() {
flagSet.String("file", "", "filename to view")
flagSet.Int("timeout", 5000, "specify the time in milliseconds to wait for a response")
err := flagSet.Parse([]string{"file","timeout"})
if err != nil {
fmt.Println(err)
}
fmt.Println("Parsed the flag ? :", flagSet.Parsed())
}
Output :
Parsed the flag ? : true
References :
Advertisement
Something interesting
Tutorials
+3.4k Golang : Fix go-cron set time not working issue
+29.5k Golang : Login(Authenticate) with Facebook example
+21.2k Golang : How to get time zone and load different time zone?
+22.2k Golang : Securing password with salt
+20.7k Android Studio : AlertDialog and EditText to get user string input example
+6.7k Golang : Reverse by word
+40.1k Golang : UDP client server read write example
+8.8k Golang : Gorilla web tool kit schema example
+5.6k Golang : Detect words using using consecutive letters in a given string
+35.9k Golang : Integer is between a range
+39k Golang : How to iterate over a []string(array)
+24.6k Golang : How to print rune, unicode, utf-8 and non-ASCII CJK(Chinese/Japanese/Korean) characters?