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 Unix/Linux : How to archive and compress entire directory ?
+4.3k Golang : Qt update UI elements with core.QCoreApplication_ProcessEvents
+9.1k Fix ERROR 1045 (28000): Access denied for user 'root'@'ip-address' (using password: YES)
+4.6k How to check with curl if my website or the asset is gzipped ?
+10.6k Golang : Handle API query by curl with Gorilla Queries example
+10.3k Golang : Simple file scaning and remove virus example
+8.8k Golang : Detect number of faces or vehicles in a photo
+20.5k Golang : Join arrays or slices example
+6.1k Golang : Validate credit card example
+14.6k Golang : Convert date format and separator yyyy-mm-dd to dd-mm-yyyy
+8.9k Golang : Find and replace data in all files recursively
+11.6k Golang : Drop cookie to visitor's browser and http.SetCookie() example