Blocks, Shadows, and Control Structures Flashcards

1
Q

Where are global variables declared?

A

In the package block.

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

What happens when a variable has the same name as an outer variable?

A

It shadows the outer variable.

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

Can you access a shadowed variable?

A

No, scope limits prevent access.

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

What does := risk when declaring variables?

A

It may unintentionally shadow an outer variable.

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

What can shadowing affect besides variables?

A

Package imports (e.g., fmt).

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

What is the universe block?

A

The outermost block containing all built-ins.

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

Are built-ins in the universe block shadowable?

A

Yes, but avoid redefining them.

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

Does an if statement need parentheses around its condition?

A

No, the condition is bare.

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

How are variables scoped in an if statement?

A

They’re limited to the if block, e.g., if n := rand.Intn(10); n == 0 {...}.

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

What is Go’s only looping construct?

A

The for loop.

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

What are the three parts of a complete for statement?

A

Init, compare, increment (e.g., i := 0; i < 10; i++).

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

How does a condition-only for statement work?

A

Like a while loop, e.g., for i < 100 {...}.

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

What defines an infinite for statement?

A

No condition, e.g., for {...}.

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

What does break do in a for loop?

A

Exits the loop immediately.

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

What does continue do in a for loop?

A

Skips to the next iteration.

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

What does a for-range statement return?

A

Index (i) and value (v) for each iteration.

17
Q

How do you ignore a value in a for-range loop?

A

Use _ (e.g., for _, v := range items {...}).

18
Q

Are map iterations in a for-range ordered?

A

No, they’re random for security.

19
Q

What does a for-range over a string iterate?

A

Runes, not bytes.

20
Q

How can you control an outer for loop from an inner one?

A

Use a label, e.g., outer: for {... continue outer}.

21
Q

How do you structure a switch statement in Go?

A

No parentheses around the condition; cases don’t fall through by default.

22
Q

How do blank switch statements work in Go?

A

They allow boolean-based conditions instead of comparing against a single value.

23
Q

When should you use switch over if in Go?

A

Use switch when making multiple related comparisons for clarity.