Booleans
A bool is only true or false, conditions must be real booleans with no truthy numbers or strings, and && and || stop evaluating as soon as the answer is known.
Compose conditions without assuming numbers or strings act as booleans.
tally should print extra detail when the user adds -v. That is a yes-or-no question, and Go answers yes-or-no questions with exactly one type, bool. Go is stricter about it than most languages — and that strictness removes a whole family of bugs.
The rule
A condition is a bool — nothing else counts — and
&&,||stop early.
A bool holds true or false, and its zero value is false. You get one from a literal, a comparison, or the logic operators:
verbose := false
long := len(word) > 7 // comparison: true or false
ok := long && !verbose // and, not
either := long || verbose // orNo truthiness. In some languages 0, "" or an empty list count as false. In Go they do not count at all:
if len(words) { } // compile error: non-boolean condition
if len(words) > 0 { } // fineThere is also no conversion between bool and numbers: int(true) does not compile. Say what you mean with a comparison.
Short-circuit. a && b does not evaluate b when a is false; a || b does not evaluate b when a is true. That makes guards possible:
verbose := len(os.Args) > 2 && os.Args[2] == "-v"If there are only two arguments, the right side is never run, so os.Args[2] never panics.
Printing uses %t or plain Println, which shows true or false. Turning text into a bool uses strconv.ParseBool("true").