Go Language If/else Statement simple use example _golang

Source: Internet
Author: User

The example in this article describes the use of the Go Language if/else statement. Share to everyone for your reference, specific as follows:

The If Else branch is straightforward in the go language.

Here's a simple example.

An If statement can have no else.

You can add another statement before a conditional statement. The variables declared in this statement are scoped to all branches.

Note: In the GO language conditional statements do not need to be parenthesized. However, the branches must have curly braces.
there are no ternary conditional statements in the go language, so even a simple conditional statement you must use the whole-body if statement (that is, there is no one in go?: An expression
)。

Sample code:

Copy Code code as follows:
Package Main
Import "FMT"
Func Main () {
If 7%2 = 0 {
Fmt. Println ("7 is even")
} else {
Fmt. Println ("7 is odd")
}
If 8%4 = 0 {
Fmt. Println ("8 is divisible by 4")
}
If num: = 9; Num < 0 {
Fmt. PRINTLN (num, "is negative")
else if num < 10 {
Fmt. PRINTLN (num, "has 1 digit")
} else {
Fmt. PRINTLN (num, "has multiple digits")
}
}

Run the test as follows:

Copy Code code as follows:
$ go Run if-else.go
7 is odd
8 is divisible by 4
9 has 1 digit

I hope this article will help you with your go language program.

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.