forked from GoesToEleven/GolangTraining
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a3753d2
commit b5c6a5f
Showing
117 changed files
with
290 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
31
23_error-handling/02_err-not-nil/03_log-set-output/main.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
38
23_error-handling/03_custom-errors/02_errors-new_var/main.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
22
23_error-handling/03_custom-errors/04_fmt-errorf_var/main.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
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.