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
+5.1k Javascript : Change page title to get viewer attention
+7.9k Golang : Get final or effective URL with Request.URL example
+12.8k Golang : Convert(cast) uintptr to string example
+8.1k Golang : Number guessing game with user input verification example
+13.1k Golang : Verify token from Google Authenticator App
+36.3k Golang : Display float in 2 decimal points and rounding up or down
+12.7k Golang : Get terminal width and height example
+9.9k Golang : Edge detection with Sobel method
+8.7k Golang : Build and compile multiple source files
+6.8k Golang : constant 20013 overflows byte error message
+13.8k Golang : Fix image: unknown format error