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
+9.5k Golang : Accessing content anonymously with Tor
+7.8k Golang : Scan files for certain pattern and rename part of the files
+27.5k Golang : Convert integer to binary, octal, hexadecimal and back to integer
+8.3k Golang : Implementing class(object-oriented programming style)
+10.1k Golang : Edge detection with Sobel method
+9.1k Golang : How to capture return values from goroutines?
+10.2k Golang : How to profile or log time spend on execution?
+26.9k Golang : Find files by extension
+17.3k Golang : How to tell if a file is compressed either gzip or zip ?
+12.3k Golang : List running EC2 instances and descriptions
+40.1k Golang : UDP client server read write example
+10k CodeIgniter : Load different view for mobile devices