|
| 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. |
0 commit comments