mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-08-12 02:16:48 +08:00
update book code
This commit is contained in:
6
eBook/exercises/chapter_13/Makefile
Executable file
6
eBook/exercises/chapter_13/Makefile
Executable file
@@ -0,0 +1,6 @@
|
||||
include $(GOROOT)/src/Make.inc
|
||||
TARG=strev
|
||||
GOFILES=\
|
||||
string_reverse.go\
|
||||
|
||||
include $(GOROOT)/src/Make.pkg
|
44
eBook/exercises/chapter_13/panic_defer.go
Executable file
44
eBook/exercises/chapter_13/panic_defer.go
Executable file
@@ -0,0 +1,44 @@
|
||||
// panic_defer.go
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
f()
|
||||
fmt.Println("Returned normally from f.")
|
||||
}
|
||||
|
||||
func f() {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
fmt.Println("Recovered in f", r)
|
||||
}
|
||||
}()
|
||||
fmt.Println("Calling g.")
|
||||
g(0)
|
||||
fmt.Println("Returned normally from g.")
|
||||
}
|
||||
|
||||
func g(i int) {
|
||||
if i > 3 {
|
||||
fmt.Println("Panicking!")
|
||||
panic(fmt.Sprintf("%v", i))
|
||||
}
|
||||
defer fmt.Println("Defer in g", i)
|
||||
fmt.Println("Printing in g", i)
|
||||
g(i + 1)
|
||||
}
|
||||
/* Output:
|
||||
Calling g.
|
||||
Printing in g 0
|
||||
Printing in g 1
|
||||
Printing in g 2
|
||||
Printing in g 3
|
||||
Panicking!
|
||||
Defer in g 3
|
||||
Defer in g 2
|
||||
Defer in g 1
|
||||
Defer in g 0
|
||||
Recovered in f 4
|
||||
Returned normally from f.
|
||||
*/
|
44
eBook/exercises/chapter_13/panic_defer_convint.go
Executable file
44
eBook/exercises/chapter_13/panic_defer_convint.go
Executable file
@@ -0,0 +1,44 @@
|
||||
// panic_defer_convint.go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
)
|
||||
|
||||
func main() {
|
||||
l := int64(15000)
|
||||
if i, err := IntFromInt64(l); err!= nil {
|
||||
fmt.Printf("The conversion of %d to an int32 resulted in an error: %s", l, err.Error())
|
||||
} else {
|
||||
fmt.Printf("%d converted to an int32 is %d", l, i)
|
||||
}
|
||||
fmt.Println()
|
||||
l = int64(math.MaxInt32 + 15000)
|
||||
if i, err := IntFromInt64(l); err!= nil {
|
||||
fmt.Printf("The conversion of %d to an int32 resulted in an error: %s", l, err.Error())
|
||||
} else {
|
||||
fmt.Printf("%d converted to an int32 is %d", l, i)
|
||||
}
|
||||
}
|
||||
|
||||
func ConvertInt64ToInt(l int64) int {
|
||||
if math.MinInt32 <= l && l <= math.MaxInt32 {
|
||||
return int(l)
|
||||
}
|
||||
panic(fmt.Sprintf("%d is out of the int32 range", l))
|
||||
}
|
||||
|
||||
func IntFromInt64(l int64) (i int, err error) {
|
||||
defer func() {
|
||||
if e := recover(); e != nil {
|
||||
err = fmt.Errorf("%v", e)
|
||||
}
|
||||
}()
|
||||
i = ConvertInt64ToInt(l)
|
||||
return i, nil
|
||||
}
|
||||
/* Output:
|
||||
15000 converted to an int32 is 15000
|
||||
The conversion of 2147498647 to an int32 resulted in an error: 2147498647 is out of the int32 range
|
||||
*/
|
34
eBook/exercises/chapter_13/recover_divbyzero.go
Executable file
34
eBook/exercises/chapter_13/recover_divbyzero.go
Executable file
@@ -0,0 +1,34 @@
|
||||
// recover_divbyzero.go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func badCall() {
|
||||
a, b := 10, 0
|
||||
n := a / b
|
||||
fmt.Println(n)
|
||||
}
|
||||
|
||||
func test() {
|
||||
defer func() {
|
||||
if e := recover(); e != nil {
|
||||
fmt.Printf("Panicing %s\r\n", e);
|
||||
}
|
||||
|
||||
}()
|
||||
badCall()
|
||||
fmt.Printf("After bad call\r\n");
|
||||
}
|
||||
|
||||
func main() {
|
||||
fmt.Printf("Calling test\r\n");
|
||||
test()
|
||||
fmt.Printf("Test completed\r\n");
|
||||
}
|
||||
/* Output:
|
||||
Calling test
|
||||
Panicing runtime error: integer divide by zero
|
||||
Test completed
|
||||
*/
|
18
eBook/exercises/chapter_13/string_reverse.go
Executable file
18
eBook/exercises/chapter_13/string_reverse.go
Executable file
@@ -0,0 +1,18 @@
|
||||
// string_reverse.go
|
||||
package strev
|
||||
|
||||
func Reverse(s string) string {
|
||||
runes := []rune(s)
|
||||
n, h := len(runes), len(runes)/2
|
||||
for i := 0; i < h; i++ {
|
||||
runes[i], runes[n-1-i] = runes[n-1-i], runes[i]
|
||||
}
|
||||
return string(runes)
|
||||
}
|
||||
|
||||
/*
|
||||
func main() {
|
||||
s := "My Test String!"
|
||||
fmt.Println(s, " --> ", Reverse(s))
|
||||
}
|
||||
*/
|
40
eBook/exercises/chapter_13/string_reverse_test.go
Executable file
40
eBook/exercises/chapter_13/string_reverse_test.go
Executable file
@@ -0,0 +1,40 @@
|
||||
// string_reverse_test.go
|
||||
package strev
|
||||
|
||||
import "testing"
|
||||
import "./strev"
|
||||
|
||||
type ReverseTest struct {
|
||||
in, out string
|
||||
}
|
||||
|
||||
var ReverseTests = []ReverseTest {
|
||||
ReverseTest{"ABCD", "DCBA"},
|
||||
ReverseTest{"CVO-AZ", "ZA-OVC"},
|
||||
ReverseTest{"Hello 世界", "界世 olleH"},
|
||||
}
|
||||
|
||||
func TestReverse(t *testing.T) {
|
||||
/*
|
||||
in := "CVO-AZ"
|
||||
out := Reverse(in)
|
||||
exp := "ZA-OVC"
|
||||
if out != exp {
|
||||
t.Errorf("Reverse of %s expects %s, but got %s", in, exp, out)
|
||||
}
|
||||
*/
|
||||
// testing with a battery of testdata:
|
||||
for _, r := range ReverseTests {
|
||||
exp := strev.Reverse(r.in)
|
||||
if r.out != exp {
|
||||
t.Errorf("Reverse of %s expects %s, but got %s", r.in, exp, r.out)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkReverse(b *testing.B) {
|
||||
s := "ABCD"
|
||||
for i:=0; i < b.N; i++ {
|
||||
strev.Reverse(s)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user