There are two types of Boolean values: True and False. The conditions in the IF and for statements are Boolean values, and the comparison operator = =, <, and so on, also produces a Boolean value. Unary operator! is a logical reverse operation, so!true is false. The Go language advocates a concise style, so we'll write the expression x = = True directly into X:if x {..}.
Boolean values can be combined with && (and), | | (OR), both operators have short-circuit characteristics, and if the left-hand expression of the operator determines the result, the expression on the right side of the operator will not be evaluated:
s! = "" && s[0] = = ' x '
If S is an empty string, then s[0] will panic, but since S is "", the expression on the left side of,&& is false, so the expression on the right side of,&& according to the short-circuit rule will not be evaluated and will not be panic.
Because && ratio | | Has a higher priority, the following conditional expression does not require parentheses:
If ' a ' <= C && c <= ' z ' | | ' A ' <= c && c <= ' Z ' | | ' 0 ' <= c && c <= ' 9 ' { //... ASCII Letter or digit ...}
There is no implicit type conversion between Boolean and numeric values (again, go is a strongly typed static language), and vice-versa. You need to explicitly use the IF:
I: = 0if B { i = 1}
If this happens frequently, you can use a conversion function:
Func Btoi (b bool) int { If B { return 1 } return 0}
The reverse operation is also simple and does not even require the use of functions, but in order to maintain symmetry with the above code:
Func Itob (i int) bool {return I! = 0}
Go Language Core technology (Volume I) of 2.4-Boolean value