Go if: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 23: Line 23:


=<tt>if</tt> with Initialization Statement=
=<tt>if</tt> with Initialization Statement=
<code>if</code> accepts an initialization statement before the expression, which is commonly used to set up a '''local variable''' that is then used in the expression:
<code>if</code> 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 subsequent <code>if</code>, <code>else</code> and <code>else if</code> [[Go_Language#if,_for,_switch_Blocks|blocks]]:
<syntaxhighlight lang='go'>
<syntaxhighlight lang='go'>
if <initialization-statement>; <expression> {
if <initialization-statement>; <expression> {
Line 31: Line 31:
<syntaxhighlight lang='go'>
<syntaxhighlight lang='go'>
if a := compute(); a > 0 {
if a := compute(); a > 0 {
   fmt.Println("positive")
   fmt.Println("positive " + a)
} else {
  fmt.Println("negative " + a)
}
}
</syntaxhighlight>
</syntaxhighlight>
The local variables declared in the initialization statement are only accessible in the expression and the subsequent [[Go_Language#if,_for,_switch_Blocks|if blocks]]. Those include <code>else if</code> and the <code>else</code> blocks.
The local variables declared in the initialization statement are only accessible in the expression and the subsequent if blocks.


This syntax supports the Go [[Go_Language_Error_Handling#If_Syntax_for_Handling_Errors|error handling idiom that relies on functions returning errors as result value]]:
This syntax supports the Go [[Go_Language_Error_Handling#If_Syntax_for_Handling_Errors|error handling idiom that relies on functions returning errors as result value]]:

Revision as of 01:08, 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 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 subsequent if, else and else if blocks:

if <initialization-statement>; <expression> {
  <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
...

Avoid else in the idiom above. Do NOT write this:

if result, err := someFunc(); err {
  // handle error
  ...
} else { // BAD, avoid the "else"
  // handle success
  ...  
}

If/else

if <condition> {
  <statements>
} else {
  <statements>
}
if x > 5 {
  println(x)
} else {
  println("something else")
}




If/else if/else

if <condition> {
  <statements>
} else if <condition> {
  <statements>
} else {
  <statements>
}
if x < 5 {
  println(x)
} else if x == 5 {
  println("is 5")
} else {
  println("something else")
}