The if init statement
In the previous lesson, the program chose a road at a fork by reading a signpost. Sometimes you need to find something out before the fork: look at the sky, say, and only then read the sign about rain. That observation matters only at this fork; further down the road it is of no use.
You can declare a variable needed for one check only before the if. But then it stays in the program until the end of main:
package main
import "fmt"
func main() {
n := 2023
r := n % 4
if r == 0 {
fmt.Println("divisible by 4")
} else {
fmt.Println("remainder", r) // remainder 3
}
}Let's walk through the program line by line:
n := 2023declares the year.r := n % 4puts intorthe remainder of2023divided by4, which is3.if r == 0: the condition is false, so theelsebranch runs and printsremainder 3.
After the if, the remainder r is no longer needed, but the name stays taken until the end of the function. Further down it can be used again by mistake, as if it held something else.
A statement before the condition
Before the condition of an if, you can write a short statement and separate it with a semicolon. Such a statement is called an init statement. An if header with one has four parts:
- the keyword
if; - the init statement, such as
r := n % 4; - a semicolon;
- the condition, such as
r == 0.
Here is the same program with an init statement:
n := 2023
if r := n % 4; r == 0 {
fmt.Println("divisible by 4")
} else {
fmt.Println("remainder", r) // remainder 3
}Let's see in what order everything runs:
- First the init statement
r := n % 4runs, andrgets3. - Then the condition
r == 0is checked. It is false. - The
elsebranch prints the remainder:remainder 3.
The init statement runs once, however many branches there are.
What goes into an init statement
Init statements come in different shapes. Let's take three cases.
A declaration with :=
Most often an init statement declares a variable with :=, like r := n % 4 above. This form suits a value needed only for the check and inside the branches.
A function call
An init statement can also call a function. The line if s := add(2, 3); s > 4 { stores the call's result in s and checks it at once.
This way the result needs no separate variable before the if, and it does not take up a name in the rest of the function.
An else if with its own init statement
An else if can have its own init statement, also before its condition and followed by a semicolon. Its variable is visible in that branch and in the ones after it, but not in the ones before.
Where the variable is visible
The scope of a variable is the part of the program where you can refer to it by name. An init statement's variable is visible:
- in the condition;
- in the
ifblock; - in each
else if; - in
else.
The scope ends together with the last branch. Inside the branches you can read and change the variable like any other. After the closing brace of the last branch it no longer exists, and the name r does not get in the way of the rest of the function.
Press "Next step" and watch r in the main frame: when it appears and when it disappears.
1–4package main · import "fmt"5func main() {6 n := 20237 if r := n % 4; r == 0 {8 fmt.Println("divisible by 4")9 } else {10 fmt.Println("remainder", r)11 }12}
n := 2023 declares the year.
Here the look-at-the-sky picture parts ways with Go. A person remembers the weather after the fork, but an init statement's variable is gone entirely after the if. Interviewers sometimes show code that uses such a variable after the if and ask what happens.
Common mistake
The variable from the init statement was used after the if:
n := 2023
if r := n % 4; r == 0 {
fmt.Println("divisible by 4")
}
fmt.Println(r) // ✗undefined: rThe error points at the line with fmt.Println(r), not at the if.
The program does not compile: outside the if there is no r, and main declares no other r. If you need the value after the if too, declare the variable before it, as in the first program.
Key points
- An init statement can come before the condition of an
if:if r := n % 4; r == 0 {. - The init statement runs once, before the condition is checked.
- Its variable is visible in the condition and in every branch, but not after the
if. - If you need the value after the
iftoo, declare the variable before it.
Subscribe to take the course
The course is free. Sign in with Telegram and subscribe to the channel to open every lesson.