There can be an initialization statement before the conditional expression. For example, assign a value to a variable.
You need to add a semicolon after the initialization statement, the format is as follows.
if init-statement; condition-expression {
// do something
}
The var keyword is not supported in the initialization statement.
The following is a complete code example.
package main
import "fmt"
func main() {
Abs := func(x int) int {
if x < 0 {
return -x
}
return x
}
if a := Abs(); a > 5 {
fmt.Println("The absolute value of -7 is greater than 5")
}
}
Below is the program output:
The absolute value of -7 is greater than 5