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
+11.8k Golang : calculate elapsed run time
+11.3k Get form post value in Go
+24.4k Golang : Create PDF file from HTML file
+5.4k Fix yum-complete-transaction error
+18.9k Golang : Fix cannot download, $GOPATH not set error
+40.6k Golang : How do I convert int to uint8?
+18.6k Golang : Calculate entire request body length during run time
+11.2k Golang : Display a text file line by line with line number example
+40.4k Golang : How to count duplicate items in slice/array?
+4.3k MariaDB/MySQL : How to get version information
+48k Golang : Upload file from web browser to server
+10.7k Golang : Fix - does not implement sort.Interface (missing Len method)