forked from GoesToEleven/GolangTraining
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
40 lines (34 loc) · 860 Bytes
/
main.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
package main
import (
"fmt"
"log"
)
type NorgateMathError struct {
lat, long string
err error
}
func (n *NorgateMathError) Error() string {
return fmt.Sprintf("a norgate math error occured: %v %v %v", n.lat, n.long, n.err)
}
func main() {
_, err := Sqrt(-10.23)
if err != nil {
log.Println(err)
}
}
func Sqrt(f float64) (float64, error) {
if f < 0 {
nme := fmt.Errorf("norgate math redux: square root of negative number: %v", f)
return 0, &NorgateMathError{"50.2289 N", "99.4656 W", nme}
}
// implementation
return 42, nil
}
// see use of structs with error type in standard library:
// http://www.goinggo.net/2014/11/error-handling-in-go-part-ii.html
//
// http://golang.org/pkg/net/#OpError
// http://golang.org/src/pkg/net/dial.go
// http://golang.org/src/pkg/net/net.go
//
// http://golang.org/src/pkg/encoding/json/decode.go