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
+48.3k Golang : How to convert JSON string to map and slice
+16.7k Golang : Delete files by extension
+10.5k Golang : Meaning of omitempty in struct's field tag
+7.6k Golang : Get YouTube playlist
+29.5k Golang : JQuery AJAX post data to server and send data back to client example
+12k Golang : Convert decimal number(integer) to IPv4 address
+6.1k PHP : How to check if an array is empty ?
+38.3k Golang : Read a text file and replace certain words
+14.6k Android Studio : Use image as AlertDialog title with custom layout example
+4.9k Javascript : How to get width and height of a div?
+11.8k Golang : Display a text file line by line with line number example
+11.1k Golang : Create S3 bucket with official aws-sdk-go package