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
+27.9k Golang : JQuery AJAX post data to server and send data back to client example
+26.4k Golang : Move file to another directory
+5.6k Golang : Derive cryptographic key from passwords with Argon2
+6.4k Golang : Individual and total number of words counter example
+8.7k Golang : Detect number of faces or vehicles in a photo
+21.1k Golang : untar or extract tar ball archive example
+10.5k Golang : Change date format to yyyy-mm-dd
+29.2k Golang : Interpolating or substituting variables in string examples
+6.2k Golang : Gorrila mux.Vars() function example
+4.8k Golang : Fix opencv.LoadHaarClassifierCascade The node does not represent a user object error
+10k Use systeminfo to find out installed Windows Hotfix(s) or updates
+16.8k Golang : Check if a directory exist or not