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
+6.7k Golang : Calculate BMI and risk category
+9.8k Golang : Test a slice of integers for odd and even numbers
+7.1k Golang : Check to see if *File is a file or directory
+28.1k Golang : Read, Write(Create) and Delete Cookie example
+13.8k Javascript : Prompt confirmation before exit
+15.4k Golang : Get current time from the Internet time server(ntp) example
+18.9k Mac OSX : Homebrew and Golang
+5.7k PHP : How to check if an array is empty ?
+7.5k Golang : get the current working directory of a running program
+8.3k Golang : Convert(cast) []byte to io.Reader type
+4.9k Golang : Customize scanner.Scanner to treat dash as part of identifier