From cd080ddda2639017f980b7fab8b86104e1c89f1f Mon Sep 17 00:00:00 2001 From: V Date: Sun, 31 Aug 2025 17:38:50 +0100 Subject: [PATCH] Sync --- tour_of_go/l5/3 | 12 +++++ tour_of_go/l5/empty-interface.go | 18 +++++++ tour_of_go/l5/error.go | 28 ++++++++++ tour_of_go/l5/ex-reader/exercise-reader.go | 16 ++++++ .../l5/ex-reader/exercise-rot-reader.go | 30 +++++++++++ tour_of_go/l5/ex-reader/go.mod | 5 ++ tour_of_go/l5/ex-reader/go.sum | 2 + tour_of_go/l5/exercise-errors.go | 41 +++++++++++++++ tour_of_go/l5/exercise-stringer.go | 19 +++++++ tour_of_go/l5/indirection.go | 30 +++++++++++ tour_of_go/l5/interfaces-continued.go | 51 +++++++++++++++++++ tour_of_go/l5/interfaces.go | 40 +++++++++++++++ tour_of_go/l5/methods-continued.go | 20 ++++++++ tour_of_go/l5/methods-pointers.go | 25 +++++++++ tour_of_go/l5/methods.go | 29 +++++++++++ tour_of_go/l5/pointers-functions.go | 25 +++++++++ tour_of_go/l5/reader.go | 21 ++++++++ tour_of_go/l5/stringer.go | 18 +++++++ tour_of_go/l5/type-assertions.go | 22 ++++++++ tour_of_go/l5/type-switches.go | 20 ++++++++ 20 files changed, 472 insertions(+) create mode 100644 tour_of_go/l5/3 create mode 100644 tour_of_go/l5/empty-interface.go create mode 100644 tour_of_go/l5/error.go create mode 100644 tour_of_go/l5/ex-reader/exercise-reader.go create mode 100644 tour_of_go/l5/ex-reader/exercise-rot-reader.go create mode 100644 tour_of_go/l5/ex-reader/go.mod create mode 100644 tour_of_go/l5/ex-reader/go.sum create mode 100644 tour_of_go/l5/exercise-errors.go create mode 100644 tour_of_go/l5/exercise-stringer.go create mode 100644 tour_of_go/l5/indirection.go create mode 100644 tour_of_go/l5/interfaces-continued.go create mode 100644 tour_of_go/l5/interfaces.go create mode 100644 tour_of_go/l5/methods-continued.go create mode 100644 tour_of_go/l5/methods-pointers.go create mode 100644 tour_of_go/l5/methods.go create mode 100644 tour_of_go/l5/pointers-functions.go create mode 100644 tour_of_go/l5/reader.go create mode 100644 tour_of_go/l5/stringer.go create mode 100644 tour_of_go/l5/type-assertions.go create mode 100644 tour_of_go/l5/type-switches.go diff --git a/tour_of_go/l5/3 b/tour_of_go/l5/3 new file mode 100644 index 0000000..3e392fb --- /dev/null +++ b/tour_of_go/l5/3 @@ -0,0 +1,12 @@ +package main + +import "fmt" + +func main() { + var i interface{} = "hello" + + s := i.(string) + fmt.Println(s) + + fmt.Printf("%v- %T\n", s, s) +} diff --git a/tour_of_go/l5/empty-interface.go b/tour_of_go/l5/empty-interface.go new file mode 100644 index 0000000..349afd1 --- /dev/null +++ b/tour_of_go/l5/empty-interface.go @@ -0,0 +1,18 @@ +package main + +import "fmt" + +func describe(i interface{}) { + fmt.Printf("(%v, %T)\n", i, i) +} + +func main() { + var i interface{} + describe(i) + + i = 42 + describe(i) + + i = "hello" + describe(i) +} diff --git a/tour_of_go/l5/error.go b/tour_of_go/l5/error.go new file mode 100644 index 0000000..2b3fde3 --- /dev/null +++ b/tour_of_go/l5/error.go @@ -0,0 +1,28 @@ +package main + +import ( + "fmt" + "time" +) + +type MyError struct { + When time.Time + What string +} + +func (e *MyError) Error() string { + return fmt.Sprintf("at %v, %s", e.When, e.What) +} + +func run() error { + return &MyError { + time.Now(), + "it didn't work", + } +} + +func main() { + if err := run(); err != nil { + fmt.Println(err) + } +} diff --git a/tour_of_go/l5/ex-reader/exercise-reader.go b/tour_of_go/l5/ex-reader/exercise-reader.go new file mode 100644 index 0000000..e269f44 --- /dev/null +++ b/tour_of_go/l5/ex-reader/exercise-reader.go @@ -0,0 +1,16 @@ +package main + +import "golang.org/x/tour/reader" + +type MyReader struct{} + +func (r MyReader) Read(b []byte) (int, error) { + for i := range(b) { + b[i] = 'A' + } + return len(b), nil +} + +func main() { + reader.Validate(MyReader{}) +} diff --git a/tour_of_go/l5/ex-reader/exercise-rot-reader.go b/tour_of_go/l5/ex-reader/exercise-rot-reader.go new file mode 100644 index 0000000..37f6b5f --- /dev/null +++ b/tour_of_go/l5/ex-reader/exercise-rot-reader.go @@ -0,0 +1,30 @@ +package main + +import ( + "io" + "os" + "strings" +) + +type rot13Reader struct { + r io.Reader +} + +func (r rot13Reader) Read(b []byte) (int, error) { + n, err := r.r.Read(b) + for i := 0; i < n; i++ { + switch { + case 'A' <= b[i] && 'Z' >= b[i]: + b[i] = 'A' + (b[i]-'A'+13)%26 + case 'a' <= b[i] && 'z' >= b[i]: + b[i] = 'a' + (b[i]-'a'+13)%26 + } + } + return n, err +} + +func main() { + s := strings.NewReader("Lbh penpxrq gur pbqr!") + r := rot13Reader{s} + io.Copy(os.Stdout, &r) +} diff --git a/tour_of_go/l5/ex-reader/go.mod b/tour_of_go/l5/ex-reader/go.mod new file mode 100644 index 0000000..feaf2d2 --- /dev/null +++ b/tour_of_go/l5/ex-reader/go.mod @@ -0,0 +1,5 @@ +module exercies-reader.go + +go 1.24.5 + +require golang.org/x/tour v0.1.0 diff --git a/tour_of_go/l5/ex-reader/go.sum b/tour_of_go/l5/ex-reader/go.sum new file mode 100644 index 0000000..cde48ea --- /dev/null +++ b/tour_of_go/l5/ex-reader/go.sum @@ -0,0 +1,2 @@ +golang.org/x/tour v0.1.0 h1:OWzbINRoGf1wwBhKdFDpYwM88NM0d1SL/Nj6PagS6YE= +golang.org/x/tour v0.1.0/go.mod h1:DUZC6G8mR1AXgXy73r8qt/G5RsefKIlSj6jBMc8b9Wc= diff --git a/tour_of_go/l5/exercise-errors.go b/tour_of_go/l5/exercise-errors.go new file mode 100644 index 0000000..a1fcdd1 --- /dev/null +++ b/tour_of_go/l5/exercise-errors.go @@ -0,0 +1,41 @@ +package main + +import ( + "fmt" +) + +type ErrNegativeSqrt float64 + +func (e ErrNegativeSqrt) Error() string { + return fmt.Sprintf("cannot Sqrt negative number: %v", float64(e)) +} + +func Sqrt(x float64) (float64, error) { + if x < 0 { + return 0, ErrNegativeSqrt(x) + } + precision := 0.00001 + old_z := 0.0 + z := x + iter := 0 + for (z - old_z) > precision { + iter += 1 + old_z := z + z -= (z*z - x) / (2*z) + if old_z - z < 0 { + if ((old_z - z) * -1) < precision { + fmt.Println("Required precision has been reached after", iter, "iterations! Square root of", x , "is", z) + return z, nil + } + } else if (old_z - z) < precision { + fmt.Println("Required precision has been reached after", iter, "iterations! Square root of", x , "is", z) + return z, nil + } + } + return z, nil +} + +func main() { + fmt.Println(Sqrt(2)) + fmt.Println(Sqrt(-2)) +} diff --git a/tour_of_go/l5/exercise-stringer.go b/tour_of_go/l5/exercise-stringer.go new file mode 100644 index 0000000..69b43fb --- /dev/null +++ b/tour_of_go/l5/exercise-stringer.go @@ -0,0 +1,19 @@ +package main + +import "fmt" + +type IPAddr [4]byte + +func (i IPAddr) String() string { + return fmt.Sprintf("%d.%d.%d.%d", i[0], i[1], i[2], i[3]) +} + +func main() { + hosts := map[string]IPAddr{ + "loopback": {127, 0, 0, 1}, + "googleDNS": {8, 8, 8, 8}, + } + for name, ip := range hosts { + fmt.Printf("%v: %v\n", name, ip) + } +} diff --git a/tour_of_go/l5/indirection.go b/tour_of_go/l5/indirection.go new file mode 100644 index 0000000..ba13bd1 --- /dev/null +++ b/tour_of_go/l5/indirection.go @@ -0,0 +1,30 @@ +package main + +import "fmt" + +type Vertex struct { + X, Y float64 +} + +func (v *Vertex) Scale(f float64) { + v.X = v.X * f + v.Y = v.Y * f +} + +func ScaleFunc(v *Vertex, f float64) { + v.X = v.X * f + v.Y = v.Y * f +} + +func main() { + v := Vertex{3, 4} + v.Scale(2) + ScaleFunc(&v, 10) + + p := &Vertex{4, 3} + p.Scale(3) + ScaleFunc(p, 8) + + fmt.Println(v, p) +} + diff --git a/tour_of_go/l5/interfaces-continued.go b/tour_of_go/l5/interfaces-continued.go new file mode 100644 index 0000000..5db9893 --- /dev/null +++ b/tour_of_go/l5/interfaces-continued.go @@ -0,0 +1,51 @@ +package main + +import ( + "fmt" + "math" +) + +type I interface { + M() +} + +type T struct { + S string +} + +func (t *T) M() { + if t == nil { + fmt.Println("") + return + } + fmt.Println(t.S) +} + +type F float64 + +func (f F) M() { + fmt.Println(f) +} +func describe(i I) { + fmt.Printf("(%v, %T)\n", i, i) +} + +func main() { + var i I + // These lines will cause a run-time error due to nil pointer dereferencing + // describe(i) + // i.M() + + var t *T + i = t + describe(i) + i.M() + + i = &T{"hello"} + describe(i) + i.M() + + i = F(math.Pi) + describe(i) + i.M() +} diff --git a/tour_of_go/l5/interfaces.go b/tour_of_go/l5/interfaces.go new file mode 100644 index 0000000..8be66f4 --- /dev/null +++ b/tour_of_go/l5/interfaces.go @@ -0,0 +1,40 @@ +package main + +import ( + "fmt" + "math" +) + +type MyFloat float64 + +type Vertex struct { + X, Y float64 +} + +type Abser interface { + Abs() float64 +} + +func (f MyFloat) Abs() float64 { + if f < 0 { + return float64(-f) + } + return float64(f) +} + +func (v *Vertex) Abs() float64 { + return math.Sqrt(v.X*v.X + v.Y*v.Y) +} + +func main() { + var a Abser + f := MyFloat(-math.Sqrt2) + v := Vertex{3, 4} + + a = f + a = &v + + // a = v + + fmt.Println(a.Abs()) +} diff --git a/tour_of_go/l5/methods-continued.go b/tour_of_go/l5/methods-continued.go new file mode 100644 index 0000000..397be47 --- /dev/null +++ b/tour_of_go/l5/methods-continued.go @@ -0,0 +1,20 @@ +package main + +import ( + "fmt" + "math" +) + +type MyFloat float64 + +func (f MyFloat) Abs() float64 { + if f < 0 { + return float64(-f) + } + return float64(f) +} + +func main() { + f := MyFloat(-math.Sqrt2) + fmt.Println(f.Abs()) +} diff --git a/tour_of_go/l5/methods-pointers.go b/tour_of_go/l5/methods-pointers.go new file mode 100644 index 0000000..7807acd --- /dev/null +++ b/tour_of_go/l5/methods-pointers.go @@ -0,0 +1,25 @@ +package main + +import ( + "fmt" + "math" +) + +type Vertex struct { + X, Y float64 +} + +func (v Vertex) Abs() float64 { + return math.Sqrt(v.X*v.X + v.Y*v.Y) +} + +func (v *Vertex) Scale(f float64) { + v.X = v.X * f + v.Y = v.Y * f +} + +func main() { + v := Vertex{3, 4} + v.Scale(10) + fmt.Println(v.Abs()) +} diff --git a/tour_of_go/l5/methods.go b/tour_of_go/l5/methods.go new file mode 100644 index 0000000..b53bc81 --- /dev/null +++ b/tour_of_go/l5/methods.go @@ -0,0 +1,29 @@ +package main + +import ( + "fmt" + "math" +) + +type Vertex struct { + X, Y float64 +} + +// This is a method - a function that implements a receiver (the part between "func" and "Abs()") + +func (v Vertex) Abs() float64 { + return math.Sqrt(v.X*v.X + v.Y*v.Y) +} + +// The method above has the same functionality as the function below + +func Abs(v Vertex) float64 { + return math.Sqrt(v.X*v.X + v.Y*v.Y) +} + +func main() { + v := Vertex{3,4} + fmt.Println(v.Abs()) + w := Vertex{3,4} + fmt.Println(Abs(w)) +} diff --git a/tour_of_go/l5/pointers-functions.go b/tour_of_go/l5/pointers-functions.go new file mode 100644 index 0000000..bc0e612 --- /dev/null +++ b/tour_of_go/l5/pointers-functions.go @@ -0,0 +1,25 @@ +package main + +import ( + "fmt" + "math" +) + +type Vertex struct { + X, Y float64 +} + +func Abs(v Vertex) float64 { + return math.Sqrt(v.X*v.X + v.Y*v.Y) +} + +func Scale(v Vertex, f float64) { + v.X = v.X * f + v.Y = v.Y * f +} + +func main() { + v := Vertex{3, 4} + Scale(v, 10) + fmt.Println(Abs(v)) +} diff --git a/tour_of_go/l5/reader.go b/tour_of_go/l5/reader.go new file mode 100644 index 0000000..f22a1f2 --- /dev/null +++ b/tour_of_go/l5/reader.go @@ -0,0 +1,21 @@ +package main + +import ( + "fmt" + "io" + "strings" +) + +func main() { + r := strings.NewReader("Hello, Reader!") + + b := make([]byte, 8) + for { + n, err := r.Read(b) + fmt.Printf("n = %v err = %v b = %v\n", n, err, b) + fmt.Printf("b[:n] = %q\n", b[:n]) + if err == io.EOF { + break + } + } +} diff --git a/tour_of_go/l5/stringer.go b/tour_of_go/l5/stringer.go new file mode 100644 index 0000000..357dcac --- /dev/null +++ b/tour_of_go/l5/stringer.go @@ -0,0 +1,18 @@ +package main + +import "fmt" + +type Person struct { + Name string + Age int +} + +func (p Person) String() string { + return fmt.Sprintf("%v (%v years)\n", p.Name, p.Age) +} + +func main() { + a := Person{"Arthur The King", 51} + b := Person{"Darth Vader", 49} + fmt.Println(a, b) +} diff --git a/tour_of_go/l5/type-assertions.go b/tour_of_go/l5/type-assertions.go new file mode 100644 index 0000000..b10a261 --- /dev/null +++ b/tour_of_go/l5/type-assertions.go @@ -0,0 +1,22 @@ +package main + +import "fmt" + +func main() { + var i interface{} = "hello" + + s := i.(string) + fmt.Println(s) + + s, ok := i.(string) + fmt.Println(s, ok) + + f, ok := i.(float64) + fmt.Println(f, ok) + + // This will cause a panic + // f = i.(float64) + // fmt.Println(f) + + fmt.Printf("Type info: %v- %T\n", s, s) +} diff --git a/tour_of_go/l5/type-switches.go b/tour_of_go/l5/type-switches.go new file mode 100644 index 0000000..a92b76f --- /dev/null +++ b/tour_of_go/l5/type-switches.go @@ -0,0 +1,20 @@ +package main + +import "fmt" + +func do(i interface{}) { + switch v := i.(type) { + case int: + fmt.Printf("Twice %v is %v\n", v, v*2) + case string: + fmt.Printf("%q is %v bytes long\n", v, len(v)) + default: + fmt.Printf("I don't know about this type %T!\n", v) + } +} + +func main() { + do(21) + do("Golang") + do(true) +}