Skip to content

Commit 1de3f29

Browse files
committed
Move code to external files
Signed-off-by: Baiju Muthukadan <[email protected]>
1 parent 23f19be commit 1de3f29

File tree

19 files changed

+302
-317
lines changed

19 files changed

+302
-317
lines changed

answers.tex

+11-213
Original file line numberDiff line numberDiff line change
@@ -11,49 +11,14 @@ \section*{Chapter 2: Quick Start}
1111

1212
\textbf{Solution:}
1313

14-
\begin{lstlisting}[numbers=none]
15-
package main
16-
17-
import "fmt"
18-
19-
func StartsCapital(s string) bool {
20-
for _, v := range "ABCDEFGHIJKLMNOPQRSTUVWXYZ" {
21-
if string(s[0]) == string(v) {
22-
return true
23-
}
24-
}
25-
return false
26-
}
27-
28-
func main() {
29-
h := StartsCapital("Hello")
30-
fmt.Println(h)
31-
w := StartsCapital("world")
32-
fmt.Println(w)
33-
}
34-
\end{lstlisting}
14+
\lstinputlisting[numbers=none]{code/answers/quick-start/capital.go}
3515

3616
\textbf{Problem 2:} Write a function to generate Fibonacci numbers
3717
below a given value.
3818

3919
\textbf{Solution:}
4020

41-
\begin{lstlisting}[numbers=none]
42-
package main
43-
44-
import "fmt"
45-
46-
func Fib(n int) {
47-
for i, j := 0, 1; i < n; i, j = i+j, i {
48-
fmt.Println(i)
49-
}
50-
51-
}
52-
53-
func main() {
54-
Fib(200)
55-
}
56-
\end{lstlisting}
21+
\lstinputlisting[numbers=none]{code/answers/quick-start/fibonacci.go}
5722

5823
\section*{Chapter 3: Control Structures}
5924

@@ -62,26 +27,7 @@ \section*{Chapter 3: Control Structures}
6227

6328
\textbf{Solution:}
6429

65-
\begin{lstlisting}[numbers=none]
66-
package main
67-
68-
import (
69-
"fmt"
70-
"time"
71-
)
72-
73-
func main() {
74-
t := time.Now()
75-
switch {
76-
case t.Hour() < 12:
77-
fmt.Println("Good morning!")
78-
case t.Hour() < 17:
79-
fmt.Println("Good afternoon.")
80-
default:
81-
fmt.Println("Good evening.")
82-
}
83-
}
84-
\end{lstlisting}
30+
\lstinputlisting[numbers=none]{code/answers/control-structures/greetings.go}
8531

8632
\textbf{Problem 2:} Write a program to check if a given number is a multiple of 2, 3, or 5.
8733

@@ -112,25 +58,7 @@ \section*{Chapter 5: Functions \& Methods}
11258

11359
\textbf{Solution:}
11460

115-
\begin{lstlisting}[numbers=none]
116-
package main
117-
118-
import "fmt"
119-
120-
type Circle struct {
121-
Radius float64
122-
}
123-
124-
// Area return the area of a circle for the given radius
125-
func (c Circle) Area() float64 {
126-
return 3.14 * c.Radius * c.Radius
127-
}
128-
129-
func main() {
130-
c := Circle{5.0}
131-
fmt.Println(c.Area())
132-
}
133-
\end{lstlisting}
61+
\lstinputlisting[numbers=none]{code/answers/functions/circle.go}
13462

13563
\section*{Chapter 6: Interfaces}
13664

@@ -144,30 +72,7 @@ \section*{Chapter 6: Interfaces}
14472

14573
\textbf{Solution:}
14674

147-
\begin{lstlisting}[numbers=none]
148-
package main
149-
150-
import "fmt"
151-
152-
type UnauthorizedError struct {
153-
UserID string
154-
}
155-
156-
func (e UnauthorizedError) Error() string {
157-
return "User not authorised: " + e.UserID
158-
}
159-
160-
func SomeAction() error {
161-
return UnauthorizedError{"jack"}
162-
}
163-
164-
func main() {
165-
err := SomeAction()
166-
if err != nil {
167-
fmt.Println(err)
168-
}
169-
}
170-
\end{lstlisting}
75+
\lstinputlisting[numbers=none]{code/answers/interfaces/error.go}
17176

17277
\section*{Chapter 7: Concurrency}
17378

@@ -176,60 +81,7 @@ \section*{Chapter 7: Concurrency}
17681

17782
\textbf{Solution:}
17883

179-
\begin{lstlisting}[numbers=none]
180-
package main
181-
182-
import (
183-
"bufio"
184-
"fmt"
185-
"os"
186-
"os/signal"
187-
"strings"
188-
"time"
189-
)
190-
191-
func watch(word, fp string) error {
192-
193-
f, err := os.Open(fp)
194-
if err != nil {
195-
return err
196-
}
197-
r := bufio.NewReader(f)
198-
defer f.Close()
199-
for {
200-
line, err := r.ReadBytes('\n')
201-
if err != nil {
202-
if err.Error() == "EOF" {
203-
time.Sleep(2 * time.Second)
204-
continue
205-
}
206-
fmt.Printf("Error: %s\n%v\n", line, err)
207-
}
208-
if strings.Contains(string(line), word) {
209-
fmt.Printf("%s: Matched: %s\n", fp, line)
210-
}
211-
time.Sleep(2 * time.Second)
212-
}
213-
}
214-
215-
func main() {
216-
word := os.Args[1]
217-
files := []string{}
218-
for _, f := range os.Args[2:len(os.Args)] {
219-
files = append(files, f)
220-
go watch(word, f)
221-
}
222-
sig := make(chan os.Signal, 1)
223-
done := make(chan bool)
224-
signal.Notify(sig, os.Interrupt)
225-
go func() {
226-
for _ = range sig {
227-
done <- true
228-
}
229-
}()
230-
<-done
231-
}
232-
\end{lstlisting}
84+
\lstinputlisting[numbers=none]{code/answers/concurrency/watchlog.go}
23385

23486
\section*{Chapter 8: Packages}
23587

@@ -241,60 +93,19 @@ \section*{Chapter 8: Packages}
24193

24294
circle.go:
24395

244-
\begin{lstlisting}[numbers=none]
245-
package shape
246-
247-
// Circle represents a circle shape
248-
type Circle struct {
249-
Radius float64
250-
}
251-
252-
// Area return the area of a circle
253-
func (c Circle) Area() float64 {
254-
return 3.14 * c.Radius * c.Radius
255-
}
256-
\end{lstlisting}
96+
\lstinputlisting[numbers=none]{code/answers/packages/docs/circle.go}
25797

25898
rectangle.go:
25999

260-
\begin{lstlisting}[numbers=none]
261-
package shape
262-
263-
// Rectangle represents a rectangle shape
264-
type Rectangle struct {
265-
Length float64
266-
Width float64
267-
}
268-
269-
// Area return the area of a rectangle
270-
func (r Rectangle) Area() float64 {
271-
return r.Length * r.Width
272-
}
273-
\end{lstlisting}
100+
\lstinputlisting[numbers=none]{code/answers/packages/docs/rectangle.go}
274101

275102
triangle.go:
276103

277-
\begin{lstlisting}[numbers=none]
278-
package shape
279-
280-
// Triangle represents a rectangle shape
281-
type Triangle struct {
282-
Breadth float64
283-
Height float64
284-
}
285-
286-
// Area return the area of a triangle
287-
func (t Triangle) Area() float64 {
288-
return (t.Breadth * t.Height)/2
289-
}
290-
\end{lstlisting}
104+
\lstinputlisting[numbers=none]{code/answers/packages/docs/triangle.go}
291105

292106
doc.go:
293107

294-
\begin{lstlisting}[numbers=none]
295-
// Package shape provides areas for different shapes
296-
// This includes circle, rectangle, and triangle.
297-
\end{lstlisting}
108+
\lstinputlisting[numbers=none]{code/answers/packages/docs/doc.go}
298109

299110
\section*{Chapter 9: Input/Output}
300111

@@ -331,20 +142,7 @@ \section*{Chapter 11: Tooling}
331142

332143
Here is the package definition for a circle object:
333144

334-
\begin{lstlisting}[numbers=none]
335-
// Package defines a circle object
336-
package circle
337-
338-
// Circle represents a circle shape
339-
type Circle struct {
340-
Radius float64
341-
}
342-
343-
// Area return the area of a circle
344-
func (c Circle) Area() float64 {
345-
return 3.14 * c.Radius * c.Radius
346-
}
347-
\end{lstlisting}
145+
\lstinputlisting[numbers=none]{code/answers/tooling/circle.go}
348146

349147
The docs can be accessed like this:
350148

code/answers/concurrency/watchlog.go

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
"fmt"
6+
"os"
7+
"os/signal"
8+
"strings"
9+
"time"
10+
)
11+
12+
func watch(word, fp string) error {
13+
14+
f, err := os.Open(fp)
15+
if err != nil {
16+
return err
17+
}
18+
r := bufio.NewReader(f)
19+
defer f.Close()
20+
for {
21+
line, err := r.ReadBytes('\n')
22+
if err != nil {
23+
if err.Error() == "EOF" {
24+
time.Sleep(2 * time.Second)
25+
continue
26+
}
27+
fmt.Printf("Error: %s\n%v\n", line, err)
28+
}
29+
if strings.Contains(string(line), word) {
30+
fmt.Printf("%s: Matched: %s\n", fp, line)
31+
}
32+
time.Sleep(2 * time.Second)
33+
}
34+
}
35+
36+
func main() {
37+
word := os.Args[1]
38+
files := []string{}
39+
for _, f := range os.Args[2:len(os.Args)] {
40+
files = append(files, f)
41+
go watch(word, f)
42+
}
43+
sig := make(chan os.Signal, 1)
44+
done := make(chan bool)
45+
signal.Notify(sig, os.Interrupt)
46+
go func() {
47+
for _ = range sig {
48+
done <- true
49+
}
50+
}()
51+
<-done
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"time"
6+
)
7+
8+
func main() {
9+
t := time.Now()
10+
switch {
11+
case t.Hour() < 12:
12+
fmt.Println("Good morning!")
13+
case t.Hour() < 17:
14+
fmt.Println("Good afternoon.")
15+
default:
16+
fmt.Println("Good evening.")
17+
}
18+
}

code/answers/functions/circle.go

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package main
2+
3+
import "fmt"
4+
5+
type Circle struct {
6+
Radius float64
7+
}
8+
9+
// Area return the area of a circle for the given radius
10+
func (c Circle) Area() float64 {
11+
return 3.14 * c.Radius * c.Radius
12+
}
13+
14+
func main() {
15+
c := Circle{5.0}
16+
fmt.Println(c.Area())
17+
}

0 commit comments

Comments
 (0)