-
Notifications
You must be signed in to change notification settings - Fork 0
/
answers.tex
168 lines (100 loc) · 4.28 KB
/
answers.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
\cleardoublepage
\phantomsection
\chapter*{Appendix A: Answers}
\addcontentsline{toc}{chapter}{Appendix A: Answers}
\markboth{Appendix A: Answers}{Appendix A: Answers}
\section*{Chapter 2: Quick Start}
\textbf{Problem 1:} Write a function to check whether the first letter
in a given string is capital letters in English (A,B,C,D etc).
\textbf{Solution:}
\lstinputlisting[numbers=none]{code/answers/quick-start/capital.go}
\textbf{Problem 2:} Write a function to generate Fibonacci numbers
below a given value.
\textbf{Solution:}
\lstinputlisting[numbers=none]{code/answers/quick-start/fibonacci.go}
\section*{Chapter 3: Control Structures}
\textbf{Problem 1:} Write a program to print greetings based on time.
Possible greetings are Good morning, Good afternoon and Good evening.
\textbf{Solution:}
\lstinputlisting[numbers=none]{code/answers/control-structures/greetings.go}
\textbf{Problem 2:} Write a program to check if the given number is divisible by 2, 3, or 5.
\textbf{Solution:}
\lstinputlisting[numbers=none]{code/answers/control-structures/multiple.go}
\section*{Chapter 4: Data Structures}
\textbf{Problem 1:} Write a program to record temperatures for different locations and check if it's freezing for a given place.
\textbf{Solution:}
\lstinputlisting[numbers=none]{code/answers/data-structures/temperature.go}
\textbf{Problem 2:} Create a map of world nations and details. The key could
be the country name and value could be an object with details including capital,
currency, and population.
\textbf{Solution:}
\lstinputlisting[numbers=none]{code/answers/data-structures/nations.go}
\section*{Chapter 5: Functions}
\textbf{Problem 1:} Write a program with a function to calculate the perimeter of a circle.
\textbf{Solution:}
\lstinputlisting[numbers=none]{code/answers/functions/circle.go}
\section*{Chapter 6: Objects}
\textbf{Problem 1:} Implement the built-in \texttt{error} interface for a custom data type. This is how the \texttt{error} interface is defined:
\begin{lstlisting}[numbers=none]
type error interface {
Error() string
}
\end{lstlisting}
\textbf{Solution:}
\lstinputlisting[numbers=none]{code/answers/objects/error.go}
\section*{Chapter 7: Concurrency}
{\bfseries Problem 1:} Write a program to watch log files and detect
any entry with a particular word.
\textbf{Solution:}
\lstinputlisting[numbers=none]{code/answers/concurrency/watchlog.go}
\section*{Chapter 8: Packages}
{\bf Problem 1:} Create a package with 3 source files and
another \textit{doc.go} for documentation. The package should provide
functions to calculate areas for circle, rectangle, and triangle.
\textbf{Solution:}
circle.go:
\lstinputlisting[numbers=none]{code/answers/packages/docs/circle.go}
rectangle.go:
\lstinputlisting[numbers=none]{code/answers/packages/docs/rectangle.go}
triangle.go:
\lstinputlisting[numbers=none]{code/answers/packages/docs/triangle.go}
doc.go:
\lstinputlisting[numbers=none]{code/answers/packages/docs/doc.go}
\section*{Chapter 9: Input/Output}
\textbf{Problem 1:} Write a program to format a complex number as used in mathematics. Example: \texttt{2 + 5i}
Use a struct like this to define the complex number:
\begin{lstlisting}[numbers=none]
type Complex struct {
Real float64
Imaginary float64
}
\end{lstlisting}
\textbf{Solution:}
\lstinputlisting[numbers=none]{code/answers/io/complex.go}
\section*{Chapter 10: Testing}
\textbf{Problem 1:} Write a test case program to fail the test and not continue with the remaining tests.
\textbf{Solution:}
\lstinputlisting[numbers=none]{code/answers/testing/failnow/failnow_test.go}
\section*{Chapter 11: Tooling}
{\bfseries Problem 1:} Write a program with exported type and methods
with documentation strings. Then print the documentation
using the \texttt{go doc} command.
\textbf{Solution:}
Here is the package definition for a circle object:
\lstinputlisting[numbers=none]{code/answers/tooling/circle.go}
The docs can be accessed like this:
\begin{lstlisting}[numbers=none]
$ go doc
package circle // import "."
Package defines a circle object
type Circle struct{ ... }
$ go doc Circle
type Circle struct {
Radius float64
}
Circle represents a circle shape
func (c Circle) Area() float64
$ go doc Circle.Area
func (c Circle) Area() float64
Area return the area of a circle
\end{lstlisting}