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
+18k How to enable MariaDB/MySQL logs ?
+17k Golang : read gzipped http response
+8.2k Golang : HTTP Server Example
+15.1k Golang : Basic authentication with .htpasswd file
+17.8k Golang : Upload/Receive file progress indicator
+4.4k Javascript : How to show different content with noscript?
+5.9k Linux/Unix/PHP : Restart PHP-FPM
+8k Golang : Load DSA public key from file example
+6.1k Facebook : How to force facebook to scrape latest URL link data?
+21.1k Golang : Convert PNG transparent background image to JPG or JPEG image
+15.7k Golang : Find location by IP address and display with Google Map
+7.2k Web : How to see your website from different countries?