I have a simple boolean flag I wish to pass args to:
import (
"flag"
...
)
var debugMode = flag.Bool("debug", false, "run in debug mode")
flag.Parse()
if *debugMode == true {
//print something
}
This code compiles and runs - but the variable is always true. I use the following call:
my_application -debug false
and it's never false. What am I doing wrong?
A Flag is a boolean variable that signals when some condition exists in a program. When a flag is set to true, it means some condition exists When a flag is set to false, it means some condition does not exist. if(score > 95) highscore = true; Here, highscore is a flag indicating that the score is above 95.
Any tag not defined in the program can be stored and accessed using the flag. Args() method. You can specify the flag by passing the index value i to flag. Args(i) .
Flags are not a best practice because they reduce the cohesion of a function. It does more than one thing. Booleans make it hard to understand what a function does based on the call site. Single argument functions and passing an object with named values improves the readability.
I spent a good hour on this. Turns out the format for specifying boolean args is:
my_application -debug=false -another_boolean_param=boolean_value
and not as stated in the question. This is tricky: non boolean parameters do NOT require the "=" character.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With