Skip to content

Commit 8f71e9f

Browse files
author
eliastor
committed
unit1 added
0 parents  commit 8f71e9f

File tree

9 files changed

+179
-0
lines changed

9 files changed

+179
-0
lines changed

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Golang in Systems Engineering 101
2+
3+
## Requirements
4+
1. [VSCode](https://code.visualstudio.com/download)
5+
2. [installed go](https://go.dev/dl)
6+
3. [Go plugin in VSCode](vscode:extension/golang.Go)
7+
8+
## Structure
9+
10+
This course consists of topics.
11+
Each topic has trainin materials, hints, quizes and excersices.
12+
13+
Each quiz should be solved, and answers should be sent as replies in Issues for respective Unit or directly to me.
14+
15+
Each exercies should be solved, and answers should be placed to respective folder sent in Pull Request to this repository. See Unit 1 Exercise 1 for more info.
16+
17+
If case of any question don't hesitate to use Issues or contact me directly.
18+
19+
Sync the main branch form time to time, new units and FAQs are being added.
20+
21+
## Unit 1: A tour of Go
22+
Here are basics of Go we should understand.
23+
24+
ETC: 7-8 hours
25+
26+
More on [unit folder](unit1)

unit1/README.md

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
# Unit 1: A tour of Go
2+
Please finish all material on https://go.dev/tour/ or https://go.dev/tour/list except "Generics" and "Concurrency"
3+
4+
Note: if you stuck with module try to read https://go.dev/blog/using-go-modules.
5+
6+
### Quiz
7+
8+
#### Q1. How will you add the number 3 to the right side?
9+
10+
`numbers := []int{1, 1, 2}`
11+
12+
1. `numbers.append(3)`
13+
2. `numbers.insert(3, 3)`
14+
3. `append(numbers, 3)`
15+
4. `numbers = append(numbers, 3)`
16+
17+
#### Q2. From where is the variable fooVar accessible if it is declared outside of any functions in a file in package fooPackage located inside module fooModule
18+
19+
1. anywhere inside `fooPackage`, not the rest of `fooModule`
20+
2. by any application that imports `fooModule`
21+
3. from anywhere in `fooModule`
22+
4. by other packages in `fooModule` as long as they import `fooPackage`
23+
24+
### Q3. What should the idiomatic name be for an interface with a single method and the signature Serve() error
25+
26+
1. Servable
27+
2. Server
28+
3. ServeInterface
29+
4. IServe
30+
31+
### Q4. Which is **not** valid loop construct?
32+
33+
1. `for i,r:=0,rand.Int(); i < r%10; i++ { ... }`
34+
2. `for { ... }`
35+
3. `{ ... } for false`
36+
4. `for _,c := range "foobar" { ... }`
37+
38+
## Excercises
39+
`E0` is for illustration how to work and submit Excercises.
40+
Let's make
41+
42+
### E0. Exercise: Loops and Functions
43+
More: https://go.dev/tour/flowcontrol/8
44+
let's create implementation in `unit1/exercises/e0/main.go`, wherer `unit1` is number of current unit, `e0` is number of current exercise.
45+
46+
If task has multiple steps to do, we assume that the last steps is final. Input values must be the same as in Exercie definition if other is not mentioned.
47+
48+
At first we've been asked to implement Sqrt function with partial implementation of Newton method and send output of computation steps on each iteration of method:
49+
50+
```go
51+
package main
52+
53+
import (
54+
"fmt"
55+
)
56+
57+
func Sqrt(x float64) float64 {
58+
z := 1.0
59+
for i:=0; i<10; i++ {
60+
delta := (z*z - x) / (2*z)
61+
z -= delta
62+
fmt.Println(z)
63+
}
64+
return z
65+
}
66+
67+
func main() {
68+
fmt.Println(Sqrt(2))
69+
}
70+
```
71+
72+
Next we've asked to change loop condition to stop once the value has stopped changing (or only changes by a very small amount). We can achieve that by adding `epsilon` constant that will be compared with `delta`.
73+
**Note that their absolute values should be compared**. One approach is to use `Abs()` function form `math` package, or we can notice that `x` argument must be positive and kae our own condition based on partial comparison of positive-only numbers:
74+
75+
```go
76+
package main
77+
78+
import (
79+
"fmt"
80+
)
81+
82+
func Sqrt(x float64) float64 {
83+
epsilon := 0.0000000000001
84+
z := 1.0
85+
for {
86+
delta := (z*z - x) / (2 * z)
87+
// or: if math.Abs(delta) < epsilon {
88+
if delta < epsilon && delta > -epsilon {
89+
break
90+
}
91+
z -= delta
92+
fmt.Println(z)
93+
}
94+
return z
95+
}
96+
97+
func main() {
98+
fmt.Println(Sqrt(2))
99+
}
100+
```
101+
102+
If you haven't managed to solve all steps publish code for the step you succeeded and make a comment in code.
103+
If you exercise requires to make multiple files or even pacakges, don't hesitate to create them in `unit1/exercises/eX/` folder
104+
105+
As soon as you implemented the code and place it into `unit1/exercises/e0/main.go`, make PR to this repo.
106+
107+
### E1. Fibonacci closure
108+
More: https://go.dev/tour/moretypes/26
109+
110+
Share your implementation `unit1/exercises/e1/main.go` in github PR.
111+
112+
### E2. Stringers
113+
More: https://go.dev/tour/methods/18
114+
115+
Share your implementation `unit1/exercises/e2/main.go` in github PR.
116+
117+
### E3. Errors
118+
More: https://go.dev/tour/methods/20
119+
120+
Share your implementation `unit1/exercises/e3/main.go` in github PR.
121+
122+
### E4. rot13Reader
123+
More: https://go.dev/tour/methods/23
124+
125+
Share your implementation `unit1/exercises/e4/main.go` in github PR.
126+
127+
### E5. Images
128+
More: https://go.dev/tour/methods/25
129+
130+
Share your implementation `unit1/exercises/e5/main.go` in github PR.

unit1/exercises/e0/.gitkeep

Whitespace-only changes.

unit1/exercises/e0/main.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
func Sqrt(x float64) float64 {
8+
epsilon := 0.0000000000001
9+
z := x / 2
10+
for {
11+
delta := (z*z - x) / (2 * z)
12+
if delta < epsilon && delta > -epsilon {
13+
break
14+
}
15+
z -= delta
16+
fmt.Println(z)
17+
}
18+
return z
19+
}
20+
21+
func main() {
22+
fmt.Println(Sqrt(2))
23+
}

unit1/exercises/e1/.gitkeep

Whitespace-only changes.

unit1/exercises/e2/.gitkeep

Whitespace-only changes.

unit1/exercises/e3/.gitkeep

Whitespace-only changes.

unit1/exercises/e4/.gitkeep

Whitespace-only changes.

unit1/exercises/e5/.gitkeep

Whitespace-only changes.

0 commit comments

Comments
 (0)