-
Notifications
You must be signed in to change notification settings - Fork 0
/
control-structures.tex
862 lines (668 loc) · 24.7 KB
/
control-structures.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
\cleardoublepage
\phantomsection
\chapter{Control Structures}
\begin{quote}
\textit{Science is what we understand well enough to explain to a
computer. Art is everything else we do.} --- Donald E. Knuth
\end{quote}
Control structure determines how the code block will get executed for the given
conditional expressions and parameters. Go provides a number of control
structures including \textit{for}, \textit{if}, \textit{switch}, \textit{defer},
and \textit{goto}. The Quick Start chapter has already introduced control
structures like \textit{if} and \textit{for}. This chapter will elaborate more
about these topics and introduce some other related topics.
\section{If}
\subsection{Basic If}
Programming involves lots of decision making based on the input
parameters. The If\index{if} control structure allows to perform a
particular action on certain condition. A conditional expressions is
what used for making decisions in the code using the If control
structure. So, the If control structure will be always associated
with a conditional expression which evaluates to a boolean value. If
the boolean value is true, the statements within the block will be
executed. Consider this example:
\begin{lstlisting}
package main
import "fmt"
func main() {
if 1 < 2 {
fmt.Println("1 is less than 2")
}
}
\end{lstlisting}
The first line starts with the \texttt{if} keyword followed by a
conditional expression and the line ends with an opening curly
bracket. If the conditional expression is getting evaluated as true,
the statements given within the curly brace will get evaluated. In
the above example, the conditional expression \texttt{1 < 2} will be
evaluated as true so the print statement given below that will get
executed. In fact, you can add any number of statements within the
braces.
\subsection{If and Else}
Sometimes you need to perform different set of action if the condition
is not true. Go provides a variation of the \texttt{if} syntax with
the \texttt{else}\index{if!else} block for that.
Consider this example with else block:
\begin{lstlisting}
package main
import "fmt"
func main() {
if 3 > 4 {
fmt.Println("3 is greater than 4")
} else {
fmt.Println("3 is not greater than 4")
}
}
\end{lstlisting}
In the above code, the code will be evaluated as false. So, the
statements within \texttt{else} block will get executed.
\subsection{Example}
Now we will go through a complete example, the task is to identify the
given number is even or odd. The input can be given as a command line
argument.
\begin{lstlisting}
package main
import (
"fmt"
"os"
"strconv"
)
func main() {
i := os.Args[1]
n, err := strconv.Atoi(i)
if err != nil {
fmt.Println("Not a number:", i)
os.Exit(1)
}
if n%2 == 0 {
fmt.Printf("%d is even\n", n)
} else {
fmt.Printf("%d is odd\n", n)
}
}
\end{lstlisting}
The \texttt{os} package has an attribute named \texttt{Args}. The value
of \texttt{Args} will be a slice of strings which contains all command line
arguments passed while running the program. As we have learned from the Quick
Start chapter, the values can be accessed using the index syntax. The value at
zero index will be the program name itself and the value at 1st index the first
argument and the value at 2nd index the second argument and so on. Since we are
expecting only one argument, you can access it using the 1st index
(\texttt{os.Args[1]}).
The \texttt{strconv} package provides a function named \texttt{Atoi}
to convert strings to integers. This function return two values, the
first one is the integer value and the second one is the error value.
If there is no error during convertion, the error value will
be \texttt{nil}. If it's a non-nil value, that indicates there is an
error occured during conversion.
The \texttt{nil} is an identifier in Go which represents the ``zero
value'' for certain built-in types. The \texttt{nil} is used as the
zero for these types: interfaces, functions, pointers, maps, slices,
and channels.
In the above example, the second expected value is an object
conforming to built-in \texttt{error} interface. We will discuss more
about errors and interfaces in later chapters. The zero value for
interface, that is \texttt{nil} is considered as there is no error.
The \texttt{Exit} function within \texttt{os} package helps to exit
the program prematurely. The argument passed will be exit status
code. Normally exit code \texttt{0} is treated as success and
non-zero value as error.
The conditional expression use the modulus operator to get remainder
and checking it is zero. If the remainder against 2 is zero, the
value will be even otherwise the value will odd.
\subsection{Else If}
There is a third alternative syntax available for the If control
structure, that is \texttt{else if} block. The Else If block get
executed if the conditional expression gives true value and previous
conditions are false. It is possible to add any number of Else If
blocks based on the requirements.
Look at this example where we have three choices based on the age
group.
\begin{lstlisting}
package main
import "fmt"
func main() {
age := 10
if age < 10 {
fmt.Println("Junior", age)
} else if age < 20 {
fmt.Println("Senior", age)
} else {
fmt.Println("Other", age)
}
}
\end{lstlisting}
In the above example, the value printed will be
either \texttt{Junior}, \texttt{Senior} or \texttt{Other}. You can
change age value and run the program again and again to see the
outputs. The Else If can be repeated here to create more choices.
\subsection{Inline Statement}
In the previous section, the variable \textit{age} was only within the
If, Else If and Else blocks. And that variable was not used used
afterwards in the function. Go provides a syntax to define a variable
along with the If where the scope of that variable will be within the
blocks. In fact, the syntax can be used for any valid Go statement.
However, it is mostly used for declaring variables.\index{if!inline
statement}
Here is an example where a variable named \texttt{money} is declared
along with the If control structure.
\begin{lstlisting}
package main
import "fmt"
func main() {
if money := 20000; money > 15000 {
fmt.Println("I am going to buy a car.", money)
} else {
fmt.Println("I am going to buy a bike.", money)
}
// can't use the variable `money` here
}
\end{lstlisting}
As mentioned above, the variable declared by the inline statement is
available only within the scope of If, Else If and Else blocks. So,
the variable \texttt{money} cannot be used outside the blocks.
It is possible to make any valid Go statement as part of the If
control structure. For example, it is possible to call a function
like this:
\begin{lstlisting}
if money := someFunction(); money > 15000 {
\end{lstlisting}
\section{For}
\subsection{Basic For}
As we have seen briefly in the Quick Start, the For\index{for} control structure
helps to create loops to repeat certain actions. The For control structure has
few syntax variants.
Consider a program to print few names.
\begin{lstlisting}
package main
import "fmt"
func main() {
names := []string{"Tom", "Polly", "Huck", "Becky"}
for i := 0; i < len(names); i++ {
fmt.Println(names[i])
}
}
\end{lstlisting}
You can save the above program in a file named \texttt{names.go}
and run it like this:
\begin{lstlisting}[numbers=none]
$ go run name.go
Tom
Polly
Huck
Becky
\end{lstlisting}
In the above example, \texttt{names} variable hold a slice of strings.
The value of \texttt{i} is initialized to zero and incremented one by
one. The \texttt{i++} statement increment the value of \texttt{i}.
The second part of \texttt{for} loop check if value of \texttt{i} is
less than length of the slice. The built-in \texttt{len} gives the
length of slice.
Other programming languages offer many ways for iterations. Some of
the examples are \textit{while} and \textit{do...while}. But in Go
using syntactic variation of \textit{for} loop meets all requirements.
Functional languages prefer to use recursion instead of iteration.
\subsection{Break Loop Prematurely}
Sometimes the iteration should be stopped prematurely on certain
condition. This can be achieved using the If control structure and
break statement. We have already studied If control structure from
the previous major section. The \texttt{break} keyword allows to
create a break statement. The break\index{break!loop} statement end the
loop immediately. Though any other code followed by For loop will be
executed.
Let's alter the previous program to stop printing after the
name \texttt{Polly} found.
\begin{lstlisting}
package main
import "fmt"
func main() {
names := []string{"Tom", "Polly", "Huck", "Becky"}
for i := 0; i < len(names); i++ {
fmt.Println(names[i])
if names[i] == "Polly" {
break
}
}
}
\end{lstlisting}
In the above example, we added an If control structure to check for
the value of name during each iteration. If the value
matches \texttt{Polly}, break statement will be executed. The break
statement makes the For loop to end immediately.
As you can see in the above code, the break statement can stand alone
without any other input. There is alternate syntax with
label\index{break!label} similar to how \textit{goto} works, which we
are going to see below. This is useful when you have multiple loops
and want to break a particular one, may be the outer loop.
To understand this better, let's consider an example. The problem is
to to change print the name given the slice until a word
with \texttt{u} found.
\begin{lstlisting}
package main
import "fmt"
func main() {
names := []string{"Tom", "Polly", "Huck", "Becky"}
Outer:
for i := 0; i < len(names); i++ {
for j := 0; j < len(names[i]); j++ {
if names[i][j] == 'u' {
break Outer
}
}
fmt.Println(names[i])
}
}
\end{lstlisting}
In the above example, we are declaring a label statement just before
the first For loop. There is an inner loop to iterate through the
name string and check for the presence of character \texttt{u}. If
the character \texttt{u} is found, then it will break the outer loop.
If the label \texttt{Outer} is not used in the break statement, then
the inner loop will be stopped.
\subsection{Partially Execute Loop Statements}
Sometimes statements within For loop should be executed on certain
iterations. Go has a \texttt{continue}\index{continue} statement to
proceed loop without executing further statements.
Let's modify the previous problem to print all names
except \texttt{Polly}.
\begin{lstlisting}
package main
import "fmt"
func main() {
names := []string{"Tom", "Polly", "Huck", "Becky"}
for i := 0; i < len(names); i++ {
if names[i] == "Polly" {
continue
}
fmt.Println(names[i])
}
}
\end{lstlisting}
In the above code, the \texttt{continue} statement makes it proceed
with next iteration in the loop without printing \texttt{Polly}.
Similar to \texttt{break} statement with label, continue also can be
used with a label\index{continue!label}. This is useful if there are
multiple loops and want to continue a particular loop, say the outer
one.
Let's consider an example where you need to print names which doesn't
have character \texttt{u} in it.
\begin{lstlisting}
package main
import "fmt"
func main() {
names := []string{"Tom", "Polly", "Huck", "Becky"}
Outer:
for i := 0; i < len(names); i++ {
for j := 0; j < len(names[i]); j++ {
if names[i][j] == 'u' {
continue Outer
}
}
fmt.Println(names[i])
}
}
\end{lstlisting}
In the above code, just before the first loop a label is declared.
Later inside the inner loop to iterate through the name string and
check for the presence of character \texttt{u}. If the
character \texttt{u} is found, then it will continue the outer loop.
If the label \texttt{Outer} is not used in the continue statement,
then the inner loop will be proceed to execute.
\subsection{For with Outside Initialization}
The statement for value initialization and the last pat to increment
value can be removed from the For control structure. The value
initialization can be moved outside For and value increment can be
moved inside loop.
The previous example can be changed like this:
\begin{lstlisting}
package main
import "fmt"
func main() {
names := []string{"Tom", "Polly", "Huck", "Becky"}
i := 0
for i < len(names) {
i++
fmt.Println(names[i])
}
}
\end{lstlisting}
In the above example, the scope of variable \texttt{i} is outside For
loop code block. Whereas in the previous section, when the variable
declared along with For loop, the scope of that variable was within
the loop code block.
\subsection{Infinite Loop}
For loop has yet another syntax variant to support infinite
loop\index{for!infinite}. You can create a loop that never ends until
explicitly stopped using break or exiting the whole program. To
create an infinite loop, you can use the \texttt{for} keyword followed
by the curly bracket.
If any variable initialization is required, that should be declared
outside the loop. Conditions can be added inside the loop.
The previous example can be changed like this:
\begin{lstlisting}
package main
import "fmt"
func main() {
names := []string{"Tom", "Polly", "Huck", "Becky"}
i := 0
for {
if i >= len(names) {
break
}
fmt.Println(names[i])
i++
}
}
\end{lstlisting}
\subsection{Range Loops}
The range\index{range!loop} clause form of the for loop iterates over
a slice or map. When looping over a slice using range, two values are
returned for each iteration. The first is the index, and the second
is a copy of the element at that index.
The previous example \texttt{for} loop can be simplified using
the \texttt{range} clause like this:
\begin{lstlisting}
package main
import "fmt"
func main() {
characters := []string{"Tom", "Polly", "Huck", "Becky"}
for _, j := range characters {
fmt.Println(j)
}
}
\end{lstlisting}
The underscore is called blank indentifier, the value assigned to that
variable will be ignored. In the above example, the index values will
be assigned to the underscore.
The range loop can be used with map. Here is an example:
\begin{lstlisting}
package main
import "fmt"
func main() {
var characters = map[string]int{
"Tom": 8,
"Polly": 51,
"Huck": 9,
"Becky": 8,
}
for name, age := range characters {
fmt.Println(name, age)
}
}
\end{lstlisting}
\section{Switch Cases}
\subsection{Basic Switch}
In addition to the \texttt{if} condition, Go provides \texttt{switch
case}\index{switch} control structure for branch instructions.
The \texttt{switch case} is more convenient if many cases need to be
handled in the branch instructions.
The below program use a switch case to print number names based on the
value.
\begin{lstlisting}
package main
import "fmt"
func main() {
v := 1
switch v {
case 0:
fmt.Println("zero")
case 1:
fmt.Println("one")
case 2:
fmt.Println("two")
default:
fmt.Println("unknown")
}
}
\end{lstlisting}
In this case, the value of \texttt{v} is \texttt{1}, so the case that
is going to execute is 2nd one. This will be the output.
\begin{lstlisting}[numbers=none]
$ go run switchbasic.go
one
\end{lstlisting}
If you change the value of \texttt{v} to \texttt{0}, it's going to
print \texttt{zero} and for \texttt{2} it will print \texttt{two}. If
the value is any number other than \texttt{0}, \texttt{1}
or \texttt{2}, it's going to print \texttt{unknown}.
\subsection{Fallthrough}
The cases are evaluated top to bottom until a match is found. If a
case is matched, the statements within that case will be executed. And
no other case will be executed unless
a \texttt{fallthrough}\index{fallthrough} statement is used.
The \texttt{fallthrough} must be the last statement within the case.
Here is a modified version with \texttt{fallthrough}
\begin{lstlisting}
package main
import "fmt"
func main() {
v := 1
switch v {
case 0:
fmt.Println("zero")
case 1:
fmt.Println("one")
fallthrough
case 2:
fmt.Println("two")
default:
fmt.Println("unknown")
}
}
\end{lstlisting}
If you run this program, it will print \texttt{one} followed
by \texttt{two}.
\begin{lstlisting}[numbers=none]
$ go run switchbasic.go
one
two
\end{lstlisting}
\subsection{Break}
As you can see from the above examples, the switch statements
break\index{break!switch} implicitly at the end of each cases.
The \texttt{fallthrough} statement can be used to passdown control to
the next case. However, sometimes execution should be stopped early
without executing all statements. This can can be achieved
using \texttt{break} statements.
Here is an example:
\begin{lstlisting}
package main
import (
"fmt"
"time"
)
func main() {
v := "Becky"
t := time.Now()
switch v {
case "Huck":
if t.Hour() < 12 {
fmt.Println("Good morning,", v)
break
}
fmt.Println("Hello,", v)
case "Becky":
if t.Hour() < 12 {
fmt.Println("Good morning,", v)
break
}
fmt.Println("Hello,", v)
default:
fmt.Println("Hello")
}
}
\end{lstlisting}
In the above example, morning time greeting is different.
\subsection{Multiple Cases}
Multple cases\index{switch!multiple cases} can be presented in
comma-separated lists.
Here is an example.
\lstinputlisting{code/control-structures/switchmultiple.go}
In this example, if any of the value is matched in the given list,
that case will be executed.
\subsection{Without Expression}
If the switch has no expression\index{switch!without expression} it
switches on true. This is useful to write an if-else-if-else chain.
Let's take the example program used earlier when Else If was
introduced:
\lstinputlisting{code/control-structures/switchnoexpression.go}
\section{Defer Statements}
Sometimes it will require to force certain things to do before a
function returns. For example, closing an opened file descriptor. Go
provides the \textit{defer}\index{defer} statements to do these kind
of cleanup actions.
A defer statement add a function call into a stack. The stack of
function call executes at the end of the surrounding function in a
last-in-first-out (LIFO) order. Defer is commonly used to perform
various clean-up actions.
Here is a simple example:
\begin{lstlisting}
package main
import (
"fmt"
"time"
)
func main() {
defer fmt.Println("world")
fmt.Println("hello")
}
\end{lstlisting}
The above program is going to print \texttt{hello} followed
by \texttt{world}.
If there are multiple \textit{defer} statements, it will execute in
last-in-first-out (LIFO) order.
Here is a simple example to demonstrate it:
\begin{lstlisting}
package main
import "fmt"
func main() {
for i := 0; i < 5; i++ {
defer fmt.Println(i)
}
}
\end{lstlisting}
The above program will print this output:
\begin{lstlisting}[numbers=none]
4
3
2
1
0
\end{lstlisting}
The arguments passed the the deferred call are evaluated immediately.
But the deferred call itself is not executed until the function
returns. Here is a simple example to demonstrate it:
\begin{lstlisting}
package main
import (
"fmt"
"time"
)
func main() {
defer func(t time.Time) {
fmt.Println(t, time.Now())
}(time.Now())
}
\end{lstlisting}
When you run the above program, you can see a small difference in
time. The \textit{defer} can also be used to recover
from \textit{panic}, which will be discussed in the next section.
\section{Deffered Panic Recover}
We have discussed the commonly used control structures including if,
for, and switch. This section is going to discuss a less commonly
used set of control structures: \textit{defer}, \textit{panic},
and \textit{recover}. We have discussed the use of the defer
statement in the previous section. In this section, you are going to
learn how to use the \textit{defer} along with \textit{panic}
and \textit{recover}.\index{panic}\index{recover}
Few important points about defer, panic, and recover:
\begin{itemize}
\item A panic causes the program stack to begin unwinding and recover can stop it
\item Deferred functions are still executed as the stack unwinds
\item If recover is called inside such a deferred function, the stack stops unwinding
\item The recover returns the value (as an \textit{interface\{\}}) that was passed to panic
\item A panic cannot be recovered by a different goroutine
\end{itemize}
Here is an example:
\lstinputlisting{code/control-structures/panic.go}
\section{Goto}
The \textit{goto}\index{goto} statement can be used to jump control to
another statement. The location to where the control should be passed
is specified using label statements. The \textit{goto} statement and
the corresponding label should be within the same function.
The \textit{goto} cannot jump to a label inside another code block.
\begin{lstlisting}
package main
import "fmt"
func main() {
num := 10
goto Marker
num = 20
Marker:
fmt.Println("Value of num:", num)
}
\end{lstlisting}
You can save the above program in a file named \texttt{goto1.go} and
run it like this:
\begin{lstlisting}
$ go run goto1.go
Value of num: 10
\end{lstlisting}
In the above code \texttt{Marker:} is a label statement. A label
statement is a valid identifier followed by a colon. A label
statement will be target for \textit{goto}, \textit{break}
or \textit{continue} statement. We will look at \textit{break}
and \textit{continue} statement when we study the For control
structure.
The \textit{goto} statement is writen using the \texttt{goto} keyword
followed by a valid label name. In the above code, immediately after
the \textit{goto} statement, there is a statement to assign a
different value to \texttt{num}. But that statement is never getting
executed as the \textit{goto} makes the program to jump to the label.
\section{Exercises}
\textbf{Exercise 1:} Print whether the number given as the command line
argument is even or odd.
\textbf{Solution:}
You can store the program with a file named \texttt{evenodd.go}.
Later you can compile this program and then you will get a binary
executable with name as \texttt{evenodd}. You can execute this
program like this:
\begin{lstlisting}[numbers=none]
./evenodd 3
3 is odd
./evenodd 4
4 is even
\end{lstlisting}
In the above program, the 3 and 4 are the command line arguments. The
command line arguments can be accessed from Go using the slice
available under \texttt{os} package. The arguments will be available
with exported name as \texttt{Args} and individual items can be
accessed using the index. The 0th index contains the program itself,
so it can be ignored. To access the 1st command argument
use \texttt{os.Args[1]}. The values will be of type string which can
be converted later.
\lstinputlisting[numbers=none]{code/control-structures/exercise1/evenodd.go}
\textbf{Exercise 2:} Write a program to print numbers below 20 which are multiples of 3 or 5.
\textbf{Solution:}
\lstinputlisting[numbers=none]{code/control-structures/exercise2/multiples.go}
\subsection{Additional Exercises}
Answers to these additional exercises are given in the Appendix A.
\textbf{Problem 1:} Write a program to print greetings based on time.
Possible greetings are Good morning, Good afternoon and Good evening.
\textbf{Problem 2:} Write a program to check if the given number is divisible by 2, 3, or 5.
\section*{Summary}
This chapter explained the control structures available in Go, except those
related to concurrency. The \textit{if} control structure was covered first, then the
\textit{for} loop was explained. The \textit{switch} cases were discussed later. Then the
\textit{defer} statement and finally, the \textit{goto} control structure was explained in
detail. This chapter also briefly explained accessing command line arguments
from the program.
Control structures are used to control the flow of execution in a program. They
allow you to execute code conditionally, repeatedly, or in a specific order.
\begin{itemize}
\item The \textit{if} control structure is used to execute code if a condition is met.
\item The \textit{for} loop is used to execute code repeatedly until a condition is met.
\item The \textit{switch} statement is used to execute code based on the value of a variable.
\item The \textit{defer} statement is used to execute code after the current function has finished executing.
\item The \textit{goto} statement is used to jump to a specific label in the code.
\end{itemize}