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.9k nginx : force all pages to be SSL
+24.7k Mac/Linux and Golang : Fix bind: address already in use error
+42.7k Golang : Use wildcard patterns with filepath.Glob() example
+15.5k Golang : read gzipped http response
+6.9k Golang : Sort words with first uppercase letter
+5.3k Golang : How to get capacity of a slice or array?
+9.7k Golang : Bubble sort example
+25.6k Golang : dial tcp: too many colons in address
+6.6k Golang : Convert(cast) io.Reader type to string
+10.7k Golang : Convert(cast) float to int
+18.4k Golang : Close channel after ticker stopped example
+4.3k Golang : Calculate half life decay example