update book code

This commit is contained in:
Unknwon
2015-03-03 12:25:25 -05:00
parent b8c82ba4e5
commit eab1d98ba8
465 changed files with 15392 additions and 1572 deletions

View File

@@ -0,0 +1,6 @@
include $(GOROOT)/src/Make.inc
TARG=strev
GOFILES=\
string_reverse.go\
include $(GOROOT)/src/Make.pkg

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

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

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

View 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))
}
*/

View 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)
}
}