forked from TannerGabriel/learning-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ErrorHandling.go
43 lines (38 loc) · 730 Bytes
/
ErrorHandling.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package main
import (
"errors"
"fmt"
"math"
)
type error interface {
Error() string
}
// Return error using multiple return statements
func Sqrt(x float64) (float64, error) {
if x < 0 {
return 0, errors.New("Math: Not possible to calculate the square root of negative number")
} else {
z := 1.0
min_delta := 0.00000000001
delta := 1.0
i := 0
for ; math.Abs(delta) > min_delta; i {
delta = (z*z - x) / (2 * z)
z = z - delta
}
fmt.Println("iterations: ", i)
return z, nil
}
}
func main() {
// Creating an error
err := errors.New("First Error")
fmt.Println("Error:", err)
// Get error as a return value
f, err := Sqrt(-1)
if err != nil {
fmt.Println(err)
} else {
fmt.Println(f)
}
}