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 Golang : How to pass data between controllers with JSON Web Token
+10.7k Google Maps URL parameters configuration
+4.9k Golang : The Tao of importing package
+22.5k Golang : Read a file into an array or slice example
+13.1k Golang : Image to ASCII art example
+6.8k Golang : How to fix html/template : "somefile" is undefined error?
+9.3k Golang : Populate slice with sequential integers example
+5.6k Fontello : How to load and use fonts?
+10.2k Golang : Create matrix with Gonum Matrix package example
+6.7k Restart Apache or Nginx web server without password prompt
+5.2k PHP : Convert CSV to JSON with YQL example
+13.5k Golang : Reverse IP address for reverse DNS lookup example