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
+7.7k Javascript : Push notifications to browser with Push.js
+12.3k Golang : calculate elapsed run time
+20.7k Nginx + FastCGI + Go Setup.
+16.6k Golang : Send email and SMTP configuration example
+5.7k Golang : Shortening import identifier
+8.1k Setting $GOPATH environment variable for Unix/Linux and Windows
+6.5k Golang : Break string into a slice of characters example
+22.2k Golang : Join arrays or slices example
+21.5k Golang : How to force compile or remove object files first before rebuild?
+36.7k Golang : Validate IP address
+26.8k Golang : How to check if a connection to database is still alive ?
+52k Golang : How to get time in milliseconds?