This article describes the use of if statements in go language. Share to everyone for your reference. The specific analysis is as follows:
The IF statement appears to be the same as in C or Java, except that it is not (even forced to use them), and {} is required.
Copy Code code as follows:
Package Main
Import (
"FMT"
"Math"
)
Func sqrt (x float64) string {
If x < 0 {
return sqrt (-X) + "I"
}
Return FMT. Sprint (Math. SQRT (x))
}
Func Main () {
Fmt. Println (sqrt (2), sqrt (-4))
}
As with for, an if statement can execute a simple statement before a condition.
The variables defined by this statement are scoped only within the IF range.
(Use V to look at the final return statement.) )
Copy Code code as follows:
Package Main
Import (
"FMT"
"Math"
)
Func pow (x, N, Lim float64) float64 {
If V: = Math. Pow (x, N); V < Lim {
Return V
}
Return Lim
}
Func Main () {
Fmt. Println (
Pow (3, 2, 10),
Pow (3, 3, 20),
)
}
Variables defined at a simple statement of if can also be used in any corresponding else block.
Copy Code code as follows:
Package Main
Import (
"FMT"
"Math"
)
Func pow (x, N, Lim float64) float64 {
If V: = Math. Pow (x, N); V < Lim {
Return V
} else {
Fmt. Printf ("%g >=%g\n", V, Lim)
}
You can't use V here, so
Return Lim
}
Func Main () {
Fmt. Println (
Pow (3, 2, 10),
Pow (3, 3, 20),
)
}
I hope this article will help you with your go language program.