Go Language Core technology (Volume I) of 2.4-Boolean value

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.