Skip to content

Commit

Permalink
Move code to external files
Browse files Browse the repository at this point in the history
Signed-off-by: Baiju Muthukadan <[email protected]>
  • Loading branch information
baijum committed Jun 8, 2023
1 parent 23f19be commit 1de3f29
Show file tree
Hide file tree
Showing 19 changed files with 302 additions and 317 deletions.
224 changes: 11 additions & 213 deletions answers.tex
Original file line number Diff line number Diff line change
Expand Up @@ -11,49 +11,14 @@ \section*{Chapter 2: Quick Start}

\textbf{Solution:}

\begin{lstlisting}[numbers=none]
package main

import "fmt"

func StartsCapital(s string) bool {
for _, v := range "ABCDEFGHIJKLMNOPQRSTUVWXYZ" {
if string(s[0]) == string(v) {
return true
}
}
return false
}

func main() {
h := StartsCapital("Hello")
fmt.Println(h)
w := StartsCapital("world")
fmt.Println(w)
}
\end{lstlisting}
\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:}

\begin{lstlisting}[numbers=none]
package main

import "fmt"

func Fib(n int) {
for i, j := 0, 1; i < n; i, j = i+j, i {
fmt.Println(i)
}

}

func main() {
Fib(200)
}
\end{lstlisting}
\lstinputlisting[numbers=none]{code/answers/quick-start/fibonacci.go}

\section*{Chapter 3: Control Structures}

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

\textbf{Solution:}

\begin{lstlisting}[numbers=none]
package main

import (
"fmt"
"time"
)

func main() {
t := time.Now()
switch {
case t.Hour() < 12:
fmt.Println("Good morning!")
case t.Hour() < 17:
fmt.Println("Good afternoon.")
default:
fmt.Println("Good evening.")
}
}
\end{lstlisting}
\lstinputlisting[numbers=none]{code/answers/control-structures/greetings.go}

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

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

\textbf{Solution:}

\begin{lstlisting}[numbers=none]
package main

import "fmt"

type Circle struct {
Radius float64
}

// Area return the area of a circle for the given radius
func (c Circle) Area() float64 {
return 3.14 * c.Radius * c.Radius
}

func main() {
c := Circle{5.0}
fmt.Println(c.Area())
}
\end{lstlisting}
\lstinputlisting[numbers=none]{code/answers/functions/circle.go}

\section*{Chapter 6: Interfaces}

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

\textbf{Solution:}

\begin{lstlisting}[numbers=none]
package main

import "fmt"

type UnauthorizedError struct {
UserID string
}

func (e UnauthorizedError) Error() string {
return "User not authorised: " + e.UserID
}

func SomeAction() error {
return UnauthorizedError{"jack"}
}

func main() {
err := SomeAction()
if err != nil {
fmt.Println(err)
}
}
\end{lstlisting}
\lstinputlisting[numbers=none]{code/answers/interfaces/error.go}

\section*{Chapter 7: Concurrency}

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

\textbf{Solution:}

\begin{lstlisting}[numbers=none]
package main

import (
"bufio"
"fmt"
"os"
"os/signal"
"strings"
"time"
)

func watch(word, fp string) error {

f, err := os.Open(fp)
if err != nil {
return err
}
r := bufio.NewReader(f)
defer f.Close()
for {
line, err := r.ReadBytes('\n')
if err != nil {
if err.Error() == "EOF" {
time.Sleep(2 * time.Second)
continue
}
fmt.Printf("Error: %s\n%v\n", line, err)
}
if strings.Contains(string(line), word) {
fmt.Printf("%s: Matched: %s\n", fp, line)
}
time.Sleep(2 * time.Second)
}
}

func main() {
word := os.Args[1]
files := []string{}
for _, f := range os.Args[2:len(os.Args)] {
files = append(files, f)
go watch(word, f)
}
sig := make(chan os.Signal, 1)
done := make(chan bool)
signal.Notify(sig, os.Interrupt)
go func() {
for _ = range sig {
done <- true
}
}()
<-done
}
\end{lstlisting}
\lstinputlisting[numbers=none]{code/answers/concurrency/watchlog.go}

\section*{Chapter 8: Packages}

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

circle.go:

\begin{lstlisting}[numbers=none]
package shape

// Circle represents a circle shape
type Circle struct {
Radius float64
}

// Area return the area of a circle
func (c Circle) Area() float64 {
return 3.14 * c.Radius * c.Radius
}
\end{lstlisting}
\lstinputlisting[numbers=none]{code/answers/packages/docs/circle.go}

rectangle.go:

\begin{lstlisting}[numbers=none]
package shape

// Rectangle represents a rectangle shape
type Rectangle struct {
Length float64
Width float64
}

// Area return the area of a rectangle
func (r Rectangle) Area() float64 {
return r.Length * r.Width
}
\end{lstlisting}
\lstinputlisting[numbers=none]{code/answers/packages/docs/rectangle.go}

triangle.go:

\begin{lstlisting}[numbers=none]
package shape

// Triangle represents a rectangle shape
type Triangle struct {
Breadth float64
Height float64
}

// Area return the area of a triangle
func (t Triangle) Area() float64 {
return (t.Breadth * t.Height)/2
}
\end{lstlisting}
\lstinputlisting[numbers=none]{code/answers/packages/docs/triangle.go}

doc.go:

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

\section*{Chapter 9: Input/Output}

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

Here is the package definition for a circle object:

\begin{lstlisting}[numbers=none]
// Package defines a circle object
package circle

// Circle represents a circle shape
type Circle struct {
Radius float64
}

// Area return the area of a circle
func (c Circle) Area() float64 {
return 3.14 * c.Radius * c.Radius
}
\end{lstlisting}
\lstinputlisting[numbers=none]{code/answers/tooling/circle.go}

The docs can be accessed like this:

Expand Down
52 changes: 52 additions & 0 deletions code/answers/concurrency/watchlog.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package main

import (
"bufio"
"fmt"
"os"
"os/signal"
"strings"
"time"
)

func watch(word, fp string) error {

f, err := os.Open(fp)
if err != nil {
return err
}
r := bufio.NewReader(f)
defer f.Close()
for {
line, err := r.ReadBytes('\n')
if err != nil {
if err.Error() == "EOF" {
time.Sleep(2 * time.Second)
continue
}
fmt.Printf("Error: %s\n%v\n", line, err)
}
if strings.Contains(string(line), word) {
fmt.Printf("%s: Matched: %s\n", fp, line)
}
time.Sleep(2 * time.Second)
}
}

func main() {
word := os.Args[1]
files := []string{}
for _, f := range os.Args[2:len(os.Args)] {
files = append(files, f)
go watch(word, f)
}
sig := make(chan os.Signal, 1)
done := make(chan bool)
signal.Notify(sig, os.Interrupt)
go func() {
for _ = range sig {
done <- true
}
}()
<-done
}
18 changes: 18 additions & 0 deletions code/answers/control-structures/greetings.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package main

import (
"fmt"
"time"
)

func main() {
t := time.Now()
switch {
case t.Hour() < 12:
fmt.Println("Good morning!")
case t.Hour() < 17:
fmt.Println("Good afternoon.")
default:
fmt.Println("Good evening.")
}
}
17 changes: 17 additions & 0 deletions code/answers/functions/circle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package main

import "fmt"

type Circle struct {
Radius float64
}

// Area return the area of a circle for the given radius
func (c Circle) Area() float64 {
return 3.14 * c.Radius * c.Radius
}

func main() {
c := Circle{5.0}
fmt.Println(c.Area())
}
Loading

0 comments on commit 1de3f29

Please sign in to comment.