fix: coding style and file format for chapter 11, 12, 13, 14 and 15.

This commit is contained in:
Bo-Yi Wu
2017-02-11 12:32:16 +08:00
parent c5413075c1
commit 4abbfabb52
63 changed files with 2662 additions and 2622 deletions

View File

@@ -1,8 +1,8 @@
package main package main
import ( import (
"fmt"
"./float64" "./float64"
"fmt"
) )
func main() { func main() {

View File

@@ -11,6 +11,7 @@ type Shaper interface {
} }
type Shape struct{} type Shape struct{}
func (sh Shape) Area() float32 { func (sh Shape) Area() float32 {
return -1 // the shape is indetermined, so we return something impossible return -1 // the shape is indetermined, so we return something impossible
} }
@@ -49,11 +50,12 @@ func main() {
c := &Circle{2.5, s} c := &Circle{2.5, s}
shapes := []Shaper{r, q, c, s} shapes := []Shaper{r, q, c, s}
fmt.Println("Looping through shapes for area ...") fmt.Println("Looping through shapes for area ...")
for n, _ := range shapes { for n := range shapes {
fmt.Println("Shape details: ", shapes[n]) fmt.Println("Shape details: ", shapes[n])
fmt.Println("Area of this shape is: ", shapes[n].Area()) fmt.Println("Area of this shape is: ", shapes[n].Area())
} }
} }
/* Output: /* Output:
Looping through shapes for area ... Looping through shapes for area ...
Shape details: {5 3} Shape details: {5 3}
@@ -65,4 +67,3 @@ Area of this shape is: 19.634954
Shape details: {} Shape details: {}
Area of this shape is: -1 Area of this shape is: -1
*/ */

View File

@@ -18,6 +18,7 @@ type AreaInterface interface {
type PeriInterface interface { type PeriInterface interface {
Perimeter() float32 Perimeter() float32
} }
func main() { func main() {
var areaIntf AreaInterface var areaIntf AreaInterface
var periIntf PeriInterface var periIntf PeriInterface

View File

@@ -41,11 +41,12 @@ func main() {
fmt.Println("Looping through shapes for area ...") fmt.Println("Looping through shapes for area ...")
// shapes := []Shaper{Shaper(r), Shaper(q), Shaper(c)} // shapes := []Shaper{Shaper(r), Shaper(q), Shaper(c)}
shapes := []Shaper{r, q, c} shapes := []Shaper{r, q, c}
for n, _ := range shapes { for n := range shapes {
fmt.Println("Shape details: ", shapes[n]) fmt.Println("Shape details: ", shapes[n])
fmt.Println("Area of this shape is: ", shapes[n].Area()) fmt.Println("Area of this shape is: ", shapes[n].Area())
} }
} }
/* Output: /* Output:
Looping through shapes for area ... Looping through shapes for area ...
Shape details: {5 3} Shape details: {5 3}

View File

@@ -2,8 +2,8 @@
package main package main
import ( import (
"fmt"
"./stack/stack" "./stack/stack"
"fmt"
) )
var st1 stack.Stack var st1 stack.Stack
@@ -21,6 +21,7 @@ func main() {
fmt.Println(item) fmt.Println(item)
} }
} }
/* Output: /* Output:
[Java C++ Python C# Ruby] [Java C++ Python C# Ruby]
100 100

View File

@@ -30,7 +30,7 @@ func main() {
} }
} }
func mapFunc(mf func(obj) obj, list []obj) ([]obj) { func mapFunc(mf func(obj) obj, list []obj) []obj {
result := make([]obj, len(list)) result := make([]obj, len(list))
for ix, v := range list { for ix, v := range list {
@@ -45,6 +45,7 @@ func mapFunc(mf func(obj) obj, list []obj) ([]obj) {
*/ */
return result return result
} }
/* Output: /* Output:
0 0
2 2

View File

@@ -27,7 +27,7 @@ func main() {
} }
} }
func mapFunc(mf func(obj) obj, list ...obj) ([]obj) { func mapFunc(mf func(obj) obj, list ...obj) []obj {
result := make([]obj, len(list)) result := make([]obj, len(list))
for ix, v := range list { for ix, v := range list {
@@ -42,6 +42,7 @@ func mapFunc(mf func(obj) obj, list ...obj) ([]obj) {
*/ */
return result return result
} }
/* Output: /* Output:
0 0
2 2

View File

@@ -18,11 +18,13 @@ func Min(data Miner) interface{} {
} }
type IntArray []int type IntArray []int
func (p IntArray) Len() int { return len(p) } func (p IntArray) Len() int { return len(p) }
func (p IntArray) ElemIx(ix int) interface{} { return p[ix] } func (p IntArray) ElemIx(ix int) interface{} { return p[ix] }
func (p IntArray) Less(i, j int) bool { return p[i] < p[j] } func (p IntArray) Less(i, j int) bool { return p[i] < p[j] }
type StringArray []string type StringArray []string
func (p StringArray) Len() int { return len(p) } func (p StringArray) Len() int { return len(p) }
func (p StringArray) ElemIx(ix int) interface{} { return p[ix] } func (p StringArray) ElemIx(ix int) interface{} { return p[ix] }
func (p StringArray) Less(i, j int) bool { return p[i] < p[j] } func (p StringArray) Less(i, j int) bool { return p[i] < p[j] }

View File

@@ -2,8 +2,8 @@
package main package main
import ( import (
"fmt"
"./min" "./min"
"fmt"
) )
func ints() { func ints() {

View File

@@ -62,6 +62,7 @@ func main() {
mag += m.Abs() mag += m.Abs()
fmt.Printf("The float64 mag is now: %f", mag) fmt.Printf("The float64 mag is now: %f", mag)
} }
/* Output: /* Output:
The length of the vector p1 is: 5.000000 The length of the vector p1 is: 5.000000
The length of the vector p2 is: 6.403124 The length of the vector p2 is: 6.403124

View File

@@ -26,13 +26,19 @@ func (day Day) String() string {
func print(args ...interface{}) { func print(args ...interface{}) {
for i, arg := range args { for i, arg := range args {
if i > 0 {os.Stdout.WriteString(" ")} if i > 0 {
os.Stdout.WriteString(" ")
}
switch a := arg.(type) { // type switch switch a := arg.(type) { // type switch
case Stringer: os.Stdout.WriteString(a.String()) case Stringer:
case int: os.Stdout.WriteString(strconv.Itoa(a)) os.Stdout.WriteString(a.String())
case string: os.Stdout.WriteString(a) case int:
os.Stdout.WriteString(strconv.Itoa(a))
case string:
os.Stdout.WriteString(a)
// more types // more types
default: os.Stdout.WriteString("???") default:
os.Stdout.WriteString("???")
} }
} }
} }
@@ -40,4 +46,5 @@ func print(args ...interface{}) {
func main() { func main() {
print(Day(1), "was", Celsius(18.36)) // Tuesday was 18.4 °C print(Day(1), "was", Celsius(18.36)) // Tuesday was 18.4 °C
} }
// Tuesday was 18.4 °C // Tuesday was 18.4 °C

View File

@@ -31,4 +31,5 @@ func main() {
var s Simple var s Simple
fmt.Println(fI(&s)) // &s is required because Get() is defined with a receiver type pointer fmt.Println(fI(&s)) // &s is required because Get() is defined with a receiver type pointer
} }
// Output: 5 // Output: 5

View File

@@ -55,6 +55,7 @@ func main() {
var r RSimple var r RSimple
fmt.Println(fI(&r)) fmt.Println(fI(&r))
} }
/* Output: /* Output:
5 5
50 50

View File

@@ -49,7 +49,6 @@ func fI(it Simpler) int {
return 0 return 0
} }
func gI(any interface{}) int { func gI(any interface{}) int {
// return any.(Simpler).Get() // unsafe, runtime panic possible // return any.(Simpler).Get() // unsafe, runtime panic possible
if v, ok := any.(Simpler); ok { if v, ok := any.(Simpler); ok {
@@ -57,6 +56,7 @@ func gI(any interface{}) int {
} }
return 0 // default value return 0 // default value
} }
/* Output: /* Output:
6 6
60 60
@@ -68,6 +68,7 @@ func main() {
var r RSimple = RSimple{60, 60} var r RSimple = RSimple{60, 60}
fmt.Println(gI(&r)) fmt.Println(gI(&r))
} }
/* Output: /* Output:
6 6
60 60

View File

@@ -2,8 +2,8 @@
package main package main
import ( import (
"fmt"
"./sort" "./sort"
"fmt"
) )
type Person struct { type Person struct {

View File

@@ -4,11 +4,11 @@
package main package main
import ( import (
"fmt"
"strconv"
"bufio"
"os"
"./stack/stack" "./stack/stack"
"bufio"
"fmt"
"os"
"strconv"
) )
func main() { func main() {

View File

@@ -1,11 +1,11 @@
package main package main
import ( import (
"os"
"io"
"fmt"
"bufio" "bufio"
"flag" "flag"
"fmt"
"io"
"os"
) )
var numberFlag = flag.Bool("n", false, "number each line") var numberFlag = flag.Bool("n", false, "number each line")

View File

@@ -3,8 +3,8 @@ package main
import ( import (
"bufio" "bufio"
"fmt"
"encoding/gob" "encoding/gob"
"fmt"
"log" "log"
"os" "os"
) )
@@ -37,5 +37,6 @@ func main() {
} }
fmt.Println(vc) fmt.Println(vc)
} }
// Output: // Output:
// {Jan Kersschot [0x12642e60 0x12642e80] none} // {Jan Kersschot [0x12642e60 0x12642e80] none}

View File

@@ -2,8 +2,8 @@
package main package main
import ( import (
"fmt";
"crypto/md5" "crypto/md5"
"fmt"
"io" "io"
) )
@@ -14,6 +14,7 @@ func main() {
fmt.Printf("Result: %x\n", hasher.Sum(b)) fmt.Printf("Result: %x\n", hasher.Sum(b))
fmt.Printf("Result: %d\n", hasher.Sum(b)) fmt.Printf("Result: %d\n", hasher.Sum(b))
} }
/* Output: /* Output:
Result: 098f6bcd4621d373cade4e832627b4f6 Result: 098f6bcd4621d373cade4e832627b4f6
Result: [9 143 107 205 70 33 211 115 202 222 78 131 38 39 180 246] Result: [9 143 107 205 70 33 211 115 202 222 78 131 38 39 180 246]

View File

@@ -4,8 +4,8 @@ package main
import ( import (
"bufio" "bufio"
"fmt" "fmt"
"log"
"io" "io"
"log"
"os" "os"
"strconv" "strconv"
"strings" "strings"
@@ -59,6 +59,7 @@ func main() {
fmt.Println(bk) fmt.Println(bk)
} }
} }
/* Output: /* Output:
We have read the following books from the file: We have read the following books from the file:
{"The ABC of Go" 25.5 1500} {"The ABC of Go" 25.5 1500}

View File

@@ -2,9 +2,9 @@
package main package main
import ( import (
"bufio"
"fmt" "fmt"
"io" "io"
"bufio"
"os" "os"
) )
@@ -27,7 +27,7 @@ func main() {
//fmt.Printf("The output was: --%s--", outputString) //fmt.Printf("The output was: --%s--", outputString)
_, err := outputWriter.WriteString(outputString) _, err := outputWriter.WriteString(outputString)
//fmt.Printf("Number of bytes written %d\n", n) //fmt.Printf("Number of bytes written %d\n", n)
if (err != nil) { if err != nil {
fmt.Println(err) fmt.Println(err)
return return
} }

View File

@@ -2,6 +2,7 @@
package stack package stack
import "strconv" import "strconv"
const LIMIT = 10 const LIMIT = 10
type Stack struct { type Stack struct {
@@ -10,7 +11,7 @@ type Stack struct {
} }
func (st *Stack) Push(n int) { func (st *Stack) Push(n int) {
if (st.ix + 1 > LIMIT) { if st.ix+1 > LIMIT {
return // stack is full! return // stack is full!
} }
st.data[st.ix] = n st.data[st.ix] = n
@@ -29,4 +30,3 @@ func (st Stack) String() string {
} }
return str return str
} }

View File

@@ -33,6 +33,7 @@ func main() {
new_page.load("Page.md") new_page.load("Page.md")
fmt.Println(string(new_page.Body)) fmt.Println(string(new_page.Body))
} }
/* Output: /* Output:
* # Page * # Page
* ## Section1 * ## Section1

View File

@@ -2,11 +2,12 @@
package main package main
import ( import (
"fmt"
"bufio" "bufio"
"fmt"
"os" "os"
"strings" "strings"
) )
var nrchars, nrwords, nrlines int var nrchars, nrwords, nrlines int
func main() { func main() {

View File

@@ -28,6 +28,7 @@ func g(i int) {
fmt.Println("Printing in g", i) fmt.Println("Printing in g", i)
g(i + 1) g(i + 1)
} }
/* Output: /* Output:
Calling g. Calling g.
Printing in g 0 Printing in g 0

View File

@@ -38,6 +38,7 @@ func IntFromInt64(l int64) (i int, err error) {
i = ConvertInt64ToInt(l) i = ConvertInt64ToInt(l)
return i, nil return i, nil
} }
/* Output: /* Output:
15000 converted to an int32 is 15000 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 The conversion of 2147498647 to an int32 resulted in an error: 2147498647 is out of the int32 range

View File

@@ -14,19 +14,20 @@ func badCall() {
func test() { func test() {
defer func() { defer func() {
if e := recover(); e != nil { if e := recover(); e != nil {
fmt.Printf("Panicing %s\r\n", e); fmt.Printf("Panicing %s\r\n", e)
} }
}() }()
badCall() badCall()
fmt.Printf("After bad call\r\n"); fmt.Printf("After bad call\r\n")
} }
func main() { func main() {
fmt.Printf("Calling test\r\n"); fmt.Printf("Calling test\r\n")
test() test()
fmt.Printf("Test completed\r\n"); fmt.Printf("Test completed\r\n")
} }
/* Output: /* Output:
Calling test Calling test
Panicing runtime error: integer divide by zero Panicing runtime error: integer divide by zero

View File

@@ -9,9 +9,9 @@ type ReverseTest struct {
} }
var ReverseTests = []ReverseTest{ var ReverseTests = []ReverseTest{
ReverseTest{"ABCD", "DCBA"}, {"ABCD", "DCBA"},
ReverseTest{"CVO-AZ", "ZA-OVC"}, {"CVO-AZ", "ZA-OVC"},
ReverseTest{"Hello 世界", "界世 olleH"}, {"Hello 世界", "界世 olleH"},
} }
func TestReverse(t *testing.T) { func TestReverse(t *testing.T) {

View File

@@ -14,6 +14,7 @@ func main() {
c <- 10 c <- 10
fmt.Println("sent", 10) fmt.Println("sent", 10)
} }
/* Output: /* Output:
sending 10 sending 10
(15 s later): (15 s later):

View File

@@ -14,6 +14,7 @@ func main() {
c <- 10 c <- 10
fmt.Println("sent", 10) fmt.Println("sent", 10)
} }
/* Output: /* Output:
sending 10 sending 10
sent 10 // prints immediately sent 10 // prints immediately

View File

@@ -39,6 +39,7 @@ func CalculatePi(n int) float64 {
func term(ch chan float64, k float64) { func term(ch chan float64, k float64) {
ch <- 4 * math.Pow(-1, k) / (2*k + 1) ch <- 4 * math.Pow(-1, k) / (2*k + 1)
} }
/* Output: /* Output:
3.14179261359579 3.14179261359579
The calculation took this amount of time: 0.028002 The calculation took this amount of time: 0.028002

View File

@@ -39,6 +39,7 @@ func term(ch chan float64, start, end int) {
} }
ch <- result ch <- result
} }
/* Output: /* Output:
3.1413926535917938 3.1413926535917938
The calculation took this amount of time: 0.002000 The calculation took this amount of time: 0.002000

View File

@@ -45,6 +45,7 @@ func BuildLazyUInt64Evaluator(evalFunc EvalFunc, initState Any) func() uint64 {
return ef().(uint64) return ef().(uint64)
} }
} }
/* Output: /* Output:
Fib nr 0: 0 Fib nr 0: 0
Fib nr 1: 1 Fib nr 1: 1

View File

@@ -3,8 +3,8 @@ package main
import ( import (
"fmt" "fmt"
"time"
"os" "os"
"time"
) )
func main() { func main() {

View File

@@ -21,6 +21,7 @@ func main() {
fmt.Println(i) fmt.Println(i)
} }
} }
/* Output: /* Output:
1 1
1 1

View File

@@ -27,6 +27,7 @@ func main() {
}() }()
fibonacci(c, quit) fibonacci(c, quit)
} }
/* Output: /* Output:
1 1
1 1

View File

@@ -28,4 +28,3 @@ func main() {
} }
} }
} }

View File

@@ -52,6 +52,7 @@ func main() {
quit <- true quit <- true
fmt.Print("done") fmt.Print("done")
} }
/* output: /* output:
3+4=7 150+250=400 3+4=7 150+250=400
done done

View File

@@ -92,6 +92,7 @@ func floatsForStrings(numbers []string) ([]float64, error) {
} }
return floats, nil return floats, nil
} }
/* Output: /* Output:
Enter a radius and an angle (in degrees), e.g., 12.5 90, or Ctrl+Z, Enter to qui Enter a radius and an angle (in degrees), e.g., 12.5 90, or Ctrl+Z, Enter to qui
t. t.

View File

@@ -28,6 +28,7 @@ func main() {
<-done <-done
} }
/* Output: /* Output:
0 0
10 10

View File

@@ -26,4 +26,5 @@ func main() {
go consume() go consume()
<-done <-done
} }
// Output: 0 1 2 3 4 5 6 7 8 9 // Output: 0 1 2 3 4 5 6 7 8 9

View File

@@ -1,10 +1,10 @@
package main package main
import ( import (
"fmt"
"os"
"net"
"bufio" "bufio"
"fmt"
"net"
"os"
"strings" "strings"
) )

View File

@@ -16,4 +16,5 @@ func main() {
var h Hello var h Hello
http.ListenAndServe("localhost:4000", h) http.ListenAndServe("localhost:4000", h)
} }
// Output in browser-window with url http://localhost:4000: Hello! // Output in browser-window with url http://localhost:4000: Hello!

View File

@@ -4,9 +4,9 @@ package main
import ( import (
"bufio" "bufio"
"fmt" "fmt"
"net/http"
"io/ioutil" "io/ioutil"
"log" "log"
"net/http"
"os" "os"
"strings" "strings"
) )

View File

@@ -2,8 +2,8 @@ package main
import ( import (
"fmt" "fmt"
"os"
"net" "net"
"os"
"strings" "strings"
) )

View File

@@ -3,11 +3,11 @@ package main
import ( import (
"fmt" "fmt"
"log"
"net/http" "net/http"
"sort" "sort"
"strings"
"strconv" "strconv"
"log" "strings"
) )
type statistics struct { type statistics struct {

View File

@@ -2,9 +2,9 @@
package main package main
import ( import (
"text/template"
"fmt" "fmt"
"log" "log"
"text/template"
) )
func main() { func main() {
@@ -22,6 +22,7 @@ func main() {
fmt.Println("The next one ought to fail.") fmt.Println("The next one ought to fail.")
template.Must(tErr.Parse(" some static text {{ .Name }")) template.Must(tErr.Parse(" some static text {{ .Name }"))
} }
/* Output: /* Output:
The first one parsed OK. The first one parsed OK.
The next one ought to fail. The next one ought to fail.

View File

@@ -2,10 +2,10 @@
package main package main
import ( import (
"net/http"
"fmt"
"encoding/json" "encoding/json"
"fmt"
"io/ioutil" "io/ioutil"
"net/http"
) )
type Status struct { type Status struct {
@@ -27,6 +27,7 @@ func main() {
json.Unmarshal(body, &user) json.Unmarshal(body, &user)
fmt.Printf("status: %s", user.Status.Text) fmt.Printf("status: %s", user.Status.Text)
} }
/* Output: /* Output:
status: Robot cars invade California, on orders from Google: status: Robot cars invade California, on orders from Google:
Google has been testing self-driving cars ... http://bit.ly/cbtpUN http://retwt.me/97p Google has been testing self-driving cars ... http://bit.ly/cbtpUN http://retwt.me/97p

View File

@@ -2,8 +2,8 @@
package main package main
import ( import (
"net/http"
"fmt" "fmt"
"net/http"
"strings" "strings"
) )