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
+4.4k Javascript : Access JSON data example
+22.3k Generate checksum for a file in Go
+8k Prevent Write failed: Broken pipe problem during ssh session with screen command
+20.4k Golang : Saving private and public key to files
+10.9k Golang : Fix - does not implement sort.Interface (missing Len method)
+54.8k Golang : Unmarshal JSON from http response
+21.8k Golang : How to run Golang application such as web server in the background or as daemon?
+4.4k Linux : sudo yum updates not working
+29.4k Golang : Get time.Duration in year, month, week or day
+22k Golang : Read directory content with filepath.Walk()
+11.4k Golang : Gorilla web tool kit secure cookie example
+51.6k Golang : How to get time in milliseconds?