Skip to content

Commit b5c6a5f

Browse files
committed
adds error handling
1 parent a3753d2 commit b5c6a5f

File tree

117 files changed

+290
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

117 files changed

+290
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
x := 1
7+
str := evalInt(x)
8+
fmt.Println(str)
9+
}
10+
11+
func evalInt(n int) string {
12+
if n > 10 {
13+
return fmt.Sprint("x is greater than 10")
14+
} else {
15+
return fmt.Sprint("x is less than 10")
16+
}
17+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
x := 1
7+
str := evalInt(x)
8+
fmt.Println(str)
9+
}
10+
11+
func evalInt(n int) string {
12+
13+
if n > 10 {
14+
return fmt.Sprint("x is greater than 10")
15+
}
16+
17+
return fmt.Sprint("x is less than 10")
18+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package main
2+
3+
import (
4+
"os"
5+
"fmt"
6+
)
7+
8+
func main() {
9+
_, err := os.Open("no-file.txt")
10+
if err != nil {
11+
fmt.Println("err happened", err)
12+
// log.Println("err happened", err)
13+
// log.Fatalln(err)
14+
// panic(err)
15+
}
16+
}
17+
18+
// Println formats using the default formats for its operands and writes to standard output.
19+
20+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package main
2+
3+
import (
4+
"os"
5+
"log"
6+
)
7+
8+
func main() {
9+
_, err := os.Open("no-file.txt")
10+
if err != nil {
11+
// fmt.Println("err happened", err)
12+
log.Println("err happened", err)
13+
// log.Fatalln(err)
14+
// panic(err)
15+
}
16+
}
17+
18+
/*
19+
Package log implements a simple logging package ... writes to standard error and prints the date and time of each logged message ... the Fatal functions call os.Exit(1) after writing the log message ... the Panic functions call panic after writing the log message.
20+
*/
21+
22+
// log.Println calls Output to print to the standard logger. Arguments are handled in the manner of fmt.Println.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
2016/02/17 04:35:28 err happened open no-file.txt: no such file or directory
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package main
2+
3+
import (
4+
"os"
5+
"log"
6+
"fmt"
7+
)
8+
9+
func init() {
10+
nf, err := os.Create("log.txt")
11+
if err != nil {
12+
fmt.Println(err)
13+
}
14+
log.SetOutput(nf)
15+
}
16+
17+
func main() {
18+
_, err := os.Open("no-file.txt")
19+
if err != nil {
20+
// fmt.Println("err happened", err)
21+
log.Println("err happened", err)
22+
// log.Fatalln(err)
23+
// panic(err)
24+
}
25+
}
26+
27+
/*
28+
Package log implements a simple logging package ... writes to standard error and prints the date and time of each logged message ... the Fatal functions call os.Exit(1) after writing the log message ... the Panic functions call panic after writing the log message.
29+
*/
30+
31+
// Println calls Output to print to the standard logger. Arguments are handled in the manner of fmt.Println.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package main
2+
3+
import (
4+
"os"
5+
"log"
6+
)
7+
8+
func main() {
9+
_, err := os.Open("no-file.txt")
10+
if err != nil {
11+
// fmt.Println("err happened", err)
12+
// log.Println("err happened", err)
13+
log.Fatalln(err)
14+
// panic(err)
15+
}
16+
}
17+
18+
/*
19+
Package log implements a simple logging package ... writes to standard error and prints the date and time of each logged message ... the Fatal functions call os.Exit(1) after writing the log message ... the Panic functions call panic after writing the log message.
20+
*/
21+
22+
// Fatalln is equivalent to Println() followed by a call to os.Exit(1).
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package main
2+
3+
import (
4+
"os"
5+
)
6+
7+
func main() {
8+
_, err := os.Open("no-file.txt")
9+
if err != nil {
10+
// fmt.Println("err happened", err)
11+
// log.Println("err happened", err)
12+
// log.Fatalln(err)
13+
panic(err)
14+
}
15+
}
16+
17+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package main
2+
3+
import (
4+
"errors"
5+
"log"
6+
)
7+
8+
func main() {
9+
_, err := Sqrt(-10)
10+
if err != nil {
11+
log.Fatalln(err)
12+
}
13+
}
14+
15+
func Sqrt(f float64) (float64, error) {
16+
if f < 0 {
17+
return 0, errors.New("norgate math: square root of negative number")
18+
}
19+
// implementation
20+
return 42, nil
21+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package main
2+
3+
import (
4+
"errors"
5+
"log"
6+
"fmt"
7+
)
8+
9+
var ErrNorgateMath = errors.New("norgate math: square root of negative number")
10+
11+
func main() {
12+
fmt.Printf("%T\n", ErrNorgateMath)
13+
_, err := Sqrt(-10)
14+
if err != nil {
15+
log.Fatalln(err)
16+
}
17+
}
18+
19+
func Sqrt(f float64) (float64, error) {
20+
if f < 0 {
21+
return 0, ErrNorgateMath
22+
}
23+
// implementation
24+
return 42, nil
25+
}
26+
27+
// see use of errors.New in standard library:
28+
// http://golang.org/src/pkg/bufio/bufio.go
29+
// http://golang.org/src/pkg/io/io.go
30+
31+
32+
33+
34+
35+
36+
37+
38+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package main
2+
3+
import (
4+
"log"
5+
"fmt"
6+
)
7+
8+
func main() {
9+
_, err := Sqrt(-10.23)
10+
if err != nil {
11+
log.Fatalln(err)
12+
}
13+
}
14+
15+
func Sqrt(f float64) (float64, error) {
16+
if f < 0 {
17+
return 0, fmt.Errorf("norgate math again: square root of negative number: %v", f)
18+
}
19+
// implementation
20+
return 42, nil
21+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package main
2+
3+
import (
4+
"log"
5+
"fmt"
6+
)
7+
8+
func main() {
9+
_, err := Sqrt(-10.23)
10+
if err != nil {
11+
log.Fatalln(err)
12+
}
13+
}
14+
15+
func Sqrt(f float64) (float64, error) {
16+
if f < 0 {
17+
ErrNorgateMath := fmt.Errorf("norgate math again: square root of negative number: %v", f)
18+
return 0, ErrNorgateMath
19+
}
20+
// implementation
21+
return 42, nil
22+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package main
2+
3+
import (
4+
"log"
5+
"fmt"
6+
)
7+
8+
type NorgateMathError struct {
9+
lat, long string
10+
err error
11+
}
12+
13+
func (n *NorgateMathError) Error() string {
14+
return fmt.Sprintf("a norgate math error occured: %v %v %v", n.lat, n.long, n.err)
15+
}
16+
17+
func main() {
18+
_, err := Sqrt(-10.23)
19+
if err != nil {
20+
log.Println(err)
21+
}
22+
}
23+
24+
func Sqrt(f float64) (float64, error) {
25+
if f < 0 {
26+
nme := fmt.Errorf("norgate math redux: square root of negative number: %v", f)
27+
return 0, &NorgateMathError{"50.2289 N", "99.4656 W", nme}
28+
}
29+
// implementation
30+
return 42, nil
31+
}
32+
33+
// see use of structs with error type in standard library:
34+
// http://www.goinggo.net/2014/11/error-handling-in-go-part-ii.html
35+
//
36+
// http://golang.org/pkg/net/#OpError
37+
// http://golang.org/src/pkg/net/dial.go
38+
// http://golang.org/src/pkg/net/net.go
39+
//
40+
// http://golang.org/src/pkg/encoding/json/decode.go
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)