Go if: Difference between revisions
No edit summary |
|||
Line 12: | Line 12: | ||
<syntaxhighlight lang='go'> | <syntaxhighlight lang='go'> | ||
if <expression> { | if <expression> { | ||
<statements> | |||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
<syntaxhighlight lang='go'> | <syntaxhighlight lang='go'> | ||
if x > 5 { | if x > 5 { | ||
println(x) | |||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 24: | Line 24: | ||
<syntaxhighlight lang='go'> | <syntaxhighlight lang='go'> | ||
if <condition> { | if <condition> { | ||
<statements> | |||
} else { | } else { | ||
<statements> | |||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
<syntaxhighlight lang='go'> | <syntaxhighlight lang='go'> | ||
if x > 5 { | if x > 5 { | ||
println(x) | |||
} else { | } else { | ||
println("something else") | |||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 41: | Line 41: | ||
<syntaxhighlight lang='go'> | <syntaxhighlight lang='go'> | ||
if <initialization-statement>; <expression> { | if <initialization-statement>; <expression> { | ||
<statements> | |||
} else if <expression> { | } else if <expression> { | ||
// variables declared by the initialization statements are visible here | |||
<statements> | |||
} else <expression> { | } else <expression> { | ||
// variables declared by the initialization statements are visible here | |||
<statements> | |||
} | } | ||
Line 53: | Line 53: | ||
<syntaxhighlight lang='go'> | <syntaxhighlight lang='go'> | ||
if a := compute(); a > 0 { | if a := compute(); a > 0 { | ||
fmt.Println("positive " + a) | |||
} else { | } else { | ||
fmt.Println("negative " + a) | |||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 65: | Line 65: | ||
var err error | var err error | ||
if result, err = someFunc(); err != nil { | if result, err = someFunc(); err != nil { | ||
// handle error | |||
return | |||
} | } | ||
// handle success | // handle success | ||
Line 75: | Line 75: | ||
<syntaxhighlight lang='go'> | <syntaxhighlight lang='go'> | ||
if result, err := someFunc(); err { | if result, err := someFunc(); err { | ||
// handle error | |||
... | |||
} else { | } else { | ||
// handle success, result is available in this block | |||
... | |||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
However, this form is less preferable than the previous one, where the function cleanly exits in the block that handles the error. | However, this form is less preferable than the previous one, where the function cleanly exits in the block that handles the error. | ||
Revision as of 01:22, 6 July 2024
External
Internal
Overview
The if
statement specifies the conditional execution of one, two or more branches according to the value of boolean expressions. Optionally, the boolean expression may be preceded by an initialization statement, which is executed before the expression is evaluated.
The statements to be executed must aways be encoded in braces. Mandatory braces encourage writing simple if
statements on multiple lines.
Simple if
if <expression> {
<statements>
}
if x > 5 {
println(x)
}
Note that because the lexer automatically inserts a semicolon after each token that may represent the end of statement, if it is followed by newline, we always must provide the opening brace on the same line as the expression.
if/else
if <condition> {
<statements>
} else {
<statements>
}
if x > 5 {
println(x)
} else {
println("something else")
}
if with Initialization Statement
if
accepts an initialization statement before the expression, which is commonly used to set up local variables that are then used in the expression, and are accessible in the entire if
statement scope, which includes the subsequent if
, else if
and else
blocks:
if <initialization-statement>; <expression> {
<statements>
} else if <expression> {
// variables declared by the initialization statements are visible here
<statements>
} else <expression> {
// variables declared by the initialization statements are visible here
<statements>
}
if a := compute(); a > 0 {
fmt.Println("positive " + a)
} else {
fmt.Println("negative " + a)
}
The local variables declared in the initialization statement are only accessible in the expression and the subsequent if blocks.
This syntax supports the Go error handling idiom that relies on functions returning errors as result value:
var result ...
var err error
if result, err = someFunc(); err != nil {
// handle error
return
}
// handle success
...
The alternative is:
if result, err := someFunc(); err {
// handle error
...
} else {
// handle success, result is available in this block
...
}
However, this form is less preferable than the previous one, where the function cleanly exits in the block that handles the error.