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.