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.6k Golang : Set or add headers for many or different handlers
+12.7k How to automatically restart your crashed Golang server
+8.9k Golang : Get current, epoch time and display by year, month and day
+5.9k Golang : Calculate BMI and risk category
+10k Golang : Proper way to test CIDR membership of an IP 4 or 6 address example
+11.7k Android Studio : Password input and reveal password example
+13.2k Golang : How to shuffle elements in array or slice?
+9.1k Golang : Read file and convert content to string
+14.3k Golang : invalid character ',' looking for beginning of value
+32.4k Golang : Call a function after some delay(time.Sleep and Tick)
+6.5k Golang : Detect sample rate, channels or latency with PortAudio
+12.8k Elastic Search : Mapping date format and sort by date