Golang : flag provided but not defined error
Problem :
Your program uses flag.Parse()
function to parse the command line arguments and everything is working fine until you attempted to parse a negative value, For example, such as calculating quadratic equation.
./program -3.0 1.0
This will cause the flag.Parse() function to recognize it a flag parameter instead of negative value. How to fix this?
Solution :
Use os.Args
instead of flag.Parse
. For example, change
flag.Parse()
arg := flag.Arg(0)
to
arg := os.Args[1]
Happy coding and hope this helps!
See also : Golang : Get command line arguments
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+28.1k Golang : Get time.Duration in year, month, week or day
+7.8k Golang : Heap sort example
+6.3k Golang : Check to see if *File is a file or directory
+18.5k Golang : Append content to a file
+15.9k Golang : Get future or past hours, minutes or seconds
+6.9k Golang Hello World Example
+2.2k Golang : Switch Redis database redis.NewClient
+9.3k Golang : Meaning of omitempty in struct's field tag
+7.3k Golang : How to check variable or object type during runtime?
+18.2k Golang : Convert seconds to human readable time format example
+12.5k Golang : Human readable time elapsed format such as 5 days ago
+28.4k Golang : How to verify uploaded file is image or allowed file types