Go Language if/else statement usage

Source: Internet
Author: User

Golang, the If/else keyword is used for conditional judgment, and if the condition is met, do something else, or do another thing.

--if statements can have no else

--You can add other statements before the conditional statement

--the scope of the variable declared in the statement is in all branches

MAKR: Conditional statements do not need parentheses in the go language, but branches must have curly braces

Grammar:

If Boolean expression {/* executes when the Boolean expression is true */} else {/* executes when the Boolean expression is false */}

Egg

Package Main

Import (
"FMT"
)
Func Main () {
/* Judge Boolean expression */
If 7%2 = = 0 {/* If the condition is true, execute the following statement */
Fmt. Println ("7 is even")
} else {/* If the condition is false, execute the following statement */
Fmt. Println ("7 is odd")
}
If 8%4 = = 0 {
Fmt. Println ("8 is divisible by 4")
} else {
Fmt. Println ("8 is odd")
}

Results: 7 is Odd8 was divisible by 4

2 notes in Go: 1, using "//" 2, using/*......*/

If you use formatted output, you need to replace println with printf.

Package Main

Import (
"FMT"
)

Func Main () {
var a int = 100
If a < 20 {
Fmt. Printf ("Less THAN 20\n")
} else {
Fmt. Printf ("More THAN 20\n")
}
Fmt. Printf ("Print A's value:%d\n", a)

}more THAN 20 Print A's value: 100

# #这里编译器可以用LiteIDE, very good, worth recommending ..... 650) this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0061.gif "alt=" J_0061.gif "/>

This article is from the "Dbaspace" blog, make sure to keep this source http://dbaspace.blog.51cto.com/6873717/1962633

Go Language if/else statement usage

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.