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
+5.8k Golang : Detect variable or constant type
+7.3k Golang : Check to see if *File is a file or directory
+18.2k Golang : Get path name to current directory or folder
+22.4k Golang : Convert Unix timestamp to UTC timestamp
+24.4k Golang : How to print rune, unicode, utf-8 and non-ASCII CJK(Chinese/Japanese/Korean) characters?
+20.3k nginx: [emerg] unknown directive "passenger_enabled"
+5.8k Facebook : How to force facebook to scrape latest URL link data?
+16.7k Golang : read gzipped http response
+16.1k Golang : How to extract links from web page ?
+7.5k Golang : Mapping Iban to Dunging alphabets
+9.6k Golang : List available AWS regions
+6.5k Golang : Warp text string by number of characters or runes example