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.5k Golang *File points to a file or directory ?
+17.5k Golang : Get future or past hours, minutes or seconds
+6.9k Golang : Decode XML data from RSS feed
+10.2k Golang : Find and replace data in all files recursively
+19.6k Golang : Close channel after ticker stopped example
+9k Golang : What is the default port number for connecting to MySQL/MariaDB database ?
+8.9k Golang : HTTP Routing with Goji example
+11.6k SSL : The certificate is not trusted because no issuer chain was provided
+8.1k Golang : Multiplexer with net/http and map
+12.3k Linux : How to install driver for 600Mbps Dual Band Wifi USB Adapter
+7k Mac OSX : Find large files by size
+5.1k Golang : Display packages names during compilation