Skip to content

Commit

Permalink
adds error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
GoesToEleven committed Feb 17, 2016
1 parent a3753d2 commit b5c6a5f
Show file tree
Hide file tree
Showing 117 changed files with 290 additions and 0 deletions.
17 changes: 17 additions & 0 deletions 23_error-handling/01_golint/01_before/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package main

import "fmt"

func main() {
x := 1
str := evalInt(x)
fmt.Println(str)
}

func evalInt(n int) string {
if n > 10 {
return fmt.Sprint("x is greater than 10")
} else {
return fmt.Sprint("x is less than 10")
}
}
18 changes: 18 additions & 0 deletions 23_error-handling/01_golint/02_after/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package main

import "fmt"

func main() {
x := 1
str := evalInt(x)
fmt.Println(str)
}

func evalInt(n int) string {

if n > 10 {
return fmt.Sprint("x is greater than 10")
}

return fmt.Sprint("x is less than 10")
}
20 changes: 20 additions & 0 deletions 23_error-handling/02_err-not-nil/01_fmt-println/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package main

import (
"os"
"fmt"
)

func main() {
_, err := os.Open("no-file.txt")
if err != nil {
fmt.Println("err happened", err)
// log.Println("err happened", err)
// log.Fatalln(err)
// panic(err)
}
}

// Println formats using the default formats for its operands and writes to standard output.


22 changes: 22 additions & 0 deletions 23_error-handling/02_err-not-nil/02_log-println/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package main

import (
"os"
"log"
)

func main() {
_, err := os.Open("no-file.txt")
if err != nil {
// fmt.Println("err happened", err)
log.Println("err happened", err)
// log.Fatalln(err)
// panic(err)
}
}

/*
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.
*/

// log.Println calls Output to print to the standard logger. Arguments are handled in the manner of fmt.Println.
1 change: 1 addition & 0 deletions 23_error-handling/02_err-not-nil/03_log-set-output/log.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2016/02/17 04:35:28 err happened open no-file.txt: no such file or directory
31 changes: 31 additions & 0 deletions 23_error-handling/02_err-not-nil/03_log-set-output/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package main

import (
"os"
"log"
"fmt"
)

func init() {
nf, err := os.Create("log.txt")
if err != nil {
fmt.Println(err)
}
log.SetOutput(nf)
}

func main() {
_, err := os.Open("no-file.txt")
if err != nil {
// fmt.Println("err happened", err)
log.Println("err happened", err)
// log.Fatalln(err)
// panic(err)
}
}

/*
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.
*/

// Println calls Output to print to the standard logger. Arguments are handled in the manner of fmt.Println.
22 changes: 22 additions & 0 deletions 23_error-handling/02_err-not-nil/04_log-fatalln/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package main

import (
"os"
"log"
)

func main() {
_, err := os.Open("no-file.txt")
if err != nil {
// fmt.Println("err happened", err)
// log.Println("err happened", err)
log.Fatalln(err)
// panic(err)
}
}

/*
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.
*/

// Fatalln is equivalent to Println() followed by a call to os.Exit(1).
17 changes: 17 additions & 0 deletions 23_error-handling/02_err-not-nil/05_panic/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package main

import (
"os"
)

func main() {
_, err := os.Open("no-file.txt")
if err != nil {
// fmt.Println("err happened", err)
// log.Println("err happened", err)
// log.Fatalln(err)
panic(err)
}
}


21 changes: 21 additions & 0 deletions 23_error-handling/03_custom-errors/01_errors-new/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package main

import (
"errors"
"log"
)

func main() {
_, err := Sqrt(-10)
if err != nil {
log.Fatalln(err)
}
}

func Sqrt(f float64) (float64, error) {
if f < 0 {
return 0, errors.New("norgate math: square root of negative number")
}
// implementation
return 42, nil
}
38 changes: 38 additions & 0 deletions 23_error-handling/03_custom-errors/02_errors-new_var/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package main

import (
"errors"
"log"
"fmt"
)

var ErrNorgateMath = errors.New("norgate math: square root of negative number")

func main() {
fmt.Printf("%T\n", ErrNorgateMath)
_, err := Sqrt(-10)
if err != nil {
log.Fatalln(err)
}
}

func Sqrt(f float64) (float64, error) {
if f < 0 {
return 0, ErrNorgateMath
}
// implementation
return 42, nil
}

// see use of errors.New in standard library:
// http://golang.org/src/pkg/bufio/bufio.go
// http://golang.org/src/pkg/io/io.go









21 changes: 21 additions & 0 deletions 23_error-handling/03_custom-errors/03_fmt-errorf/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package main

import (
"log"
"fmt"
)

func main() {
_, err := Sqrt(-10.23)
if err != nil {
log.Fatalln(err)
}
}

func Sqrt(f float64) (float64, error) {
if f < 0 {
return 0, fmt.Errorf("norgate math again: square root of negative number: %v", f)
}
// implementation
return 42, nil
}
22 changes: 22 additions & 0 deletions 23_error-handling/03_custom-errors/04_fmt-errorf_var/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package main

import (
"log"
"fmt"
)

func main() {
_, err := Sqrt(-10.23)
if err != nil {
log.Fatalln(err)
}
}

func Sqrt(f float64) (float64, error) {
if f < 0 {
ErrNorgateMath := fmt.Errorf("norgate math again: square root of negative number: %v", f)
return 0, ErrNorgateMath
}
// implementation
return 42, nil
}
40 changes: 40 additions & 0 deletions 23_error-handling/03_custom-errors/05_custom-type/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package main

import (
"log"
"fmt"
)

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
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.
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.
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 comments on commit b5c6a5f

Please sign in to comment.