This is a creation in Article, where the information may have evolved or changed.
The branch structure of if and else in the go language is very clear.
Package Mainimport "FMT" Func Main () { //This is a basic example if 7%2 = = 0 { fmt. Println ("7 is even") } else { fmt. Println ("7 is Odd") } //Only if conditions are in case if 8%4 = = 0 { fmt. Println ("8 is divisible by 4") } //If condition can contain an initialization expression, the variable in the expression //is a local variable of this condition to determine the structure, and the variable is available in all branches If num: = 9; Num < 0 { FMT. PRINTLN (num, "is negative") } else if num < { FMT. PRINTLN (num, "has 1 digit") } else { fmt. PRINTLN (num, "has multiple digits") }}
In the go language, you don't need braces on both sides of the condition, but the braces on both sides of the conditional execution code are indispensable.
Output
$ go Run if-else.go 7 is ODD8-divisible by-has 1 digit
In addition, there is no ternary expression for "?:" In the Go language, so you need to use the full if declaration even in the underlying condition.
Next example: Go by Example:switch.
English original