/
error_returnval.go
executable file
·57 lines (51 loc) · 1.54 KB
/
error_returnval.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// error_returnval.go
package main
import (
"errors"
"fmt"
"math"
)
func main() {
fmt.Print("First example with -1: ")
ret1, err1 := MySqrt(-1)
if err1 != nil {
fmt.Println("Error! Return values are: ", ret1, err1)
} else {
fmt.Println("It's ok! Return values are: ", ret1, err1)
}
fmt.Print("Second example with 5: ")
//you could also write it like this
if ret2, err2 := MySqrt(5); err2 != nil {
fmt.Println("Error! Return values are: ", ret2, err2)
} else {
fmt.Println("It's ok! Return values are: ", ret2, err2)
}
// named return variables:
fmt.Println(MySqrt2(5))
}
func MySqrt(f float64) (float64, error) {
//return an error as second parameter if invalid input
if f < 0 {
return float64(math.NaN()), errors.New("I won't be able to do a sqrt of negative number!")
}
//otherwise use default square root function
return math.Sqrt(f), nil
}
//name the return variables - by default it will have 'zero-ed' values i.e. numbers are 0, string is empty, etc.
func MySqrt2(f float64) (ret float64, err error) {
if f < 0 {
//then you can use those variables in code
ret = float64(math.NaN())
err = errors.New("I won't be able to do a sqrt of negative number!")
} else {
ret = math.Sqrt(f)
//err is not assigned, so it gets default value nil
}
//automatically return the named return variables ret and err
return
}
/* Output:
First example with -1: Error! Return values are: NaN I won't be able to do a sqrt of negative number!
Second example with 5: It's ok! Return values are: 2.23606797749979 <nil>
2.23606797749979 <nil>
*/