A Tour of Go - Basics: Flow control statements: for, if, else, switch and defer Flashcards

1
Q

How many loop constructs are there in Go?

A

one

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is a for loop?

A

The basic for loop has three components separated by semicolons:

  • the init statement: executed before the first iteration
  • the condition expression: evaluated before every iteration
  • the post statement: executed at the end of every iteration

The init statement will often be a short variable declaration, and the variables declared there are visible only in the scope of the for statement.

The loop will stop iterating once the boolean condition evaluates to false.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is a forever loop?

A
for {
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Which for loop components are optional?

A

The init statement and the post statement. When omitted the semi-colons can be dropped.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Give an example of a simple if statement?

A
if a == b {
    fmt.Println("a equals b")
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Give an example of an if with a short statement?

A
if v := math.Pow(x, n); v < lim {
    return v
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Give and example of an if else statement

A
if v := math.Pow(x, n); v < lim {
    return v
} else {
    fmt.Printf("%g >= %g\n", v, lim)
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

A switch statement is a …

A

A switch statement is a shorter way to write a sequence of if - else statements. It runs the first case whose value is equal to the condition expression.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Switch cases evaluate cases ….

A

Switch cases evaluate cases from top to bottom, stopping when a case succeeds.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Go’s switch cases ….

A

Go’s switch cases need not be constants, and the values involved need not be integers.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Switch without a condition is …

A

Switch without a condition is the same as switch true.

This construct can be a clean way to write long if-then-else chains.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Give an example of a switch statement

A
switch os := runtime.GOOS; os {
	case "darwin":
		fmt.Println("OS X.")
	case "linux":
		fmt.Println("Linux.")
	default:
		fmt.Printf("%s.\n", os)
	}
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

A defer statement ….

A

A defer statement defers the execution of a function until the surrounding function returns.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

The deferred call’s arguments ….

A

The deferred call’s arguments are evaluated immediately, but the function call is not executed until the surrounding function returns.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Deferred function calls are ….

A

Deferred function calls are pushed onto a stack. When a function returns, its deferred calls are executed in last-in-first-out order.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly