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