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
+4.7k Golang : fmt.Println prints out empty data from struct
+8.7k Golang : Qt get screen resolution and display on center example
+47.3k Golang : Upload file from web browser to server
+9.1k Golang : Ordinal and Ordinalize a given number to the English ordinal numeral
+7.2k Findstr command the Grep equivalent for Windows
+6.5k Golang : Create zip/ePub file without compression(use Store algorithm)
+8.5k Random number generation with crypto/rand in Go
+15.3k Golang : Generate QR codes for Google Authenticator App and fix "Cannot interpret QR code" error
+6.5k Golang : Check to see if *File is a file or directory
+9.9k Golang : Command line file upload program to server example
+4.8k Linux/Unix/PHP : Restart PHP-FPM
+11.4k Golang : calculate elapsed run time