Go by Example:if/else

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

The branch structure of if and else in the go language is very clear.

Package Mainimport "FMT" Func Main () {    //This is a basic example    if 7%2 = = 0 {        fmt. Println ("7 is even")    } else {        fmt. Println ("7 is Odd")    }    //Only if conditions are in case    if 8%4 = = 0 {        fmt. Println ("8 is divisible by 4")    }    //If condition can contain an initialization expression, the variable in the expression    //is a local variable of this condition to determine the structure, and the variable is available    in all branches If num: = 9; Num < 0 {        FMT. PRINTLN (num, "is negative")    } else if num < {        FMT. PRINTLN (num, "has 1 digit")    } else {        fmt. PRINTLN (num, "has multiple digits")    }}

In the go language, you don't need braces on both sides of the condition, but the braces on both sides of the conditional execution code are indispensable.

Output

$ go Run if-else.go 7 is ODD8-divisible by-has 1 digit

In addition, there is no ternary expression for "?:" In the Go language, so you need to use the full if declaration even in the underlying condition.


Next example: Go by Example:switch.

English original

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.