Save Error interface experiments.
authorsgf <sgf.dma@gmail.com>
Tue, 21 Jun 2022 21:20:04 +0000 (00:20 +0300)
committersgf <sgf.dma@gmail.com>
Tue, 21 Jun 2022 21:20:04 +0000 (00:20 +0300)
err.go [new file with mode: 0644]

diff --git a/err.go b/err.go
new file mode 100644 (file)
index 0000000..5aa483e
--- /dev/null
+++ b/err.go
@@ -0,0 +1,80 @@
+package main
+
+import (
+        "fmt"
+)
+
+type ErrNegativeSqrt float64
+
+func (e *ErrNegativeSqrt) Error() string {
+        return fmt.Sprintf("cannot Sqrt negative number: %v", *e)
+}
+
+func myPrint(i interface{}) string {
+       switch v := i.(type) {
+       case fmt.Stringer:
+               return "made by Stringer " + v.String()
+       case error:
+               return "Huh, that's an error " + v.Error()
+       default:
+               return "a huy ego znait.."
+       }
+}
+
+/*
+func Sqrt(x float64) (float64, error) {
+        z := 1.0
+        y := 0.0
+        for i := 0; i < 10 && z != x*x && z != y; i++ {
+                fmt.Println(i, z)
+                y = z
+                z -= (z*z - x) / (2 * z)
+        }
+        return z, nil
+}
+*/
+
+type ErrType1 struct {}
+
+func (e *ErrType1) Error() string {
+    return "ErrType1"
+}
+
+type ErrType2 struct {}
+
+func (e ErrType2) Error() string {
+    return "ErrType2"
+}
+
+func someFunc(i int) error {
+    if i == 1 {
+        return &ErrType1{}
+    } else if i == 2 {
+        return ErrType2{}
+    } else {
+        return &ErrType2{}
+    }
+}
+
+func main() {
+        //fmt.Println(Sqrt(2))
+        //fmt.Println(Sqrt(-2))
+        fmt.Println("****")
+        var v ErrNegativeSqrt = 2.0
+        var err error = &v
+        //fmt.Println(myPrint(v))
+        fmt.Println("a", err.Error(), "b")
+        fmt.Println("****")
+
+        r := someFunc(3)
+        switch r.(type) {
+        case *ErrType1:
+            fmt.Println("*errt1")
+        case ErrType2:
+            fmt.Println("errt2")
+        case *ErrType2:
+            fmt.Println("*errt2")
+        default:
+            fmt.Println("huy")
+        }
+}