Merge pull request #317 from appleboy/patch-3

fix: coding style and file format for chapter 6.
This commit is contained in:
无闻
2017-02-10 23:30:42 -05:00
committed by GitHub
11 changed files with 290 additions and 279 deletions

View File

@@ -1,13 +1,15 @@
package main package main
import "fmt" import "fmt"
func main() { func main() {
printrec(1) printrec(1)
} }
func printrec(i int) { func printrec(i int) {
if i>10 { return } if i > 10 {
printrec(i+1) return
fmt.Printf("%d ", i) }
} printrec(i + 1)
fmt.Printf("%d ", i)
}

View File

@@ -1,17 +1,17 @@
// compose.go // compose.go
package main package main
import ( import (
"fmt" "fmt"
"math" "math"
) )
func Compose(f, g func(x float64) float64) func(x float64) float64 { func Compose(f, g func(x float64) float64) func(x float64) float64 {
return func(x float64) float64 { // closure return func(x float64) float64 { // closure
return f(g(x)) return f(g(x))
} }
} }
func main() { func main() {
fmt.Print(Compose(math.Sin, math.Cos)(0.5)) // output: 0.7691963548410085 fmt.Print(Compose(math.Sin, math.Cos)(0.5)) // output: 0.7691963548410085
} }

View File

@@ -1,56 +1,57 @@
// error_returnval.go // error_returnval.go
package main package main
import ( import (
"errors" "errors"
"fmt" "fmt"
"math" "math"
) )
func main() { func main() {
fmt.Print("First example with -1: ") fmt.Print("First example with -1: ")
ret1, err1 := MySqrt(-1) ret1, err1 := MySqrt(-1)
if err1 != nil { if err1 != nil {
fmt.Println("Error! Return values are: ", ret1, err1) fmt.Println("Error! Return values are: ", ret1, err1)
} else { } else {
fmt.Println("It's ok! Return values are: ", ret1, err1) fmt.Println("It's ok! Return values are: ", ret1, err1)
} }
fmt.Print("Second example with 5: ") fmt.Print("Second example with 5: ")
//you could also write it like this //you could also write it like this
if ret2, err2 := MySqrt(5); err2 != nil { if ret2, err2 := MySqrt(5); err2 != nil {
fmt.Println("Error! Return values are: ", ret2, err2) fmt.Println("Error! Return values are: ", ret2, err2)
} else { } else {
fmt.Println("It's ok! Return values are: ", ret2, err2) fmt.Println("It's ok! Return values are: ", ret2, err2)
} }
// named return variables: // named return variables:
fmt.Println(MySqrt2(5)) fmt.Println(MySqrt2(5))
} }
func MySqrt(f float64) (float64, error) { func MySqrt(f float64) (float64, error) {
//return an error as second parameter if invalid input //return an error as second parameter if invalid input
if (f < 0) { if f < 0 {
return float64(math.NaN()), errors.New("I won't be able to do a sqrt of negative number!") return float64(math.NaN()), errors.New("I won't be able to do a sqrt of negative number!")
} }
//otherwise use default square root function //otherwise use default square root function
return math.Sqrt(f), nil return math.Sqrt(f), nil
} }
//name the return variables - by default it will have 'zero-ed' values i.e. numbers are 0, string is empty, etc. //name the return variables - by default it will have 'zero-ed' values i.e. numbers are 0, string is empty, etc.
func MySqrt2(f float64) (ret float64, err error) { func MySqrt2(f float64) (ret float64, err error) {
if (f < 0) { if f < 0 {
//then you can use those variables in code //then you can use those variables in code
ret = float64(math.NaN()) ret = float64(math.NaN())
err = errors.New("I won't be able to do a sqrt of negative number!") err = errors.New("I won't be able to do a sqrt of negative number!")
} else { } else {
ret = math.Sqrt(f) ret = math.Sqrt(f)
//err is not assigned, so it gets default value nil //err is not assigned, so it gets default value nil
} }
//automatically return the named return variables ret and err //automatically return the named return variables ret and err
return return
} }
/* Output:
First example with -1: Error! Return values are: NaN I won't be able to do a sqrt of negative number! /* Output:
Second example with 5: It's ok! Return values are: 2.23606797749979 <nil> First example with -1: Error! Return values are: NaN I won't be able to do a sqrt of negative number!
2.23606797749979 <nil> Second example with 5: It's ok! Return values are: 2.23606797749979 <nil>
*/ 2.23606797749979 <nil>
*/

View File

@@ -1,34 +1,34 @@
// factorial.go // factorial.go
package main package main
import ( import (
"fmt" "fmt"
) )
func main() { func main() {
for i := uint64(0); i < uint64(30); i++ { for i := uint64(0); i < uint64(30); i++ {
fmt.Printf("Factorial of %d is %d\n", i, Factorial(i)) fmt.Printf("Factorial of %d is %d\n", i, Factorial(i))
} }
} }
/* unnamed return variables: /* unnamed return variables:
func Factorial(n uint64) uint64 { func Factorial(n uint64) uint64 {
if n > 0 { if n > 0 {
return n * Factorial(n-1) return n * Factorial(n-1)
} }
return 1 return 1
} }
*/ */
// named return variables: // named return variables:
func Factorial(n uint64) (fac uint64) { func Factorial(n uint64) (fac uint64) {
fac = 1 fac = 1
if n > 0 { if n > 0 {
fac = n * Factorial(n-1) fac = n * Factorial(n-1)
return return
} }
return return
} }
// int: correct results till 12! // int: correct results till 12!
// uint64: correct results till 21! // uint64: correct results till 21!

View File

@@ -1,24 +1,24 @@
package main package main
import "fmt" import "fmt"
func main() { func main() {
pos := 4 pos := 4
result, pos := fibonacci(pos) result, pos := fibonacci(pos)
fmt.Printf("the %d-th fibonacci number is: %d\n", pos, result) fmt.Printf("the %d-th fibonacci number is: %d\n", pos, result)
pos = 10 pos = 10
result, pos = fibonacci(pos) result, pos = fibonacci(pos)
fmt.Printf("the %d-th fibonacci number is: %d\n", pos, result) fmt.Printf("the %d-th fibonacci number is: %d\n", pos, result)
} }
func fibonacci(n int) (val, pos int) { func fibonacci(n int) (val, pos int) {
if n <= 1 { if n <= 1 {
val = 1 val = 1
} else { } else {
v1, _ := fibonacci(n-1) v1, _ := fibonacci(n - 1)
v2, _ := fibonacci(n-2) v2, _ := fibonacci(n - 2)
val = v1 + v2 val = v1 + v2
} }
pos = n pos = n
return return
} }

View File

@@ -1,20 +1,20 @@
package main package main
// fib returns a function that returns // fib returns a function that returns
// successive Fibonacci numbers. // successive Fibonacci numbers.
func fib() func() int { func fib() func() int {
a, b := 1, 1 a, b := 1, 1
return func() int { return func() int {
a, b = b, a+b a, b = b, a+b
return b return b
} }
} }
func main() { func main() {
f := fib() f := fib()
// Function calls are evaluated left-to-right. // Function calls are evaluated left-to-right.
// println(f(), f(), f(), f(), f()) // println(f(), f(), f(), f(), f())
for i:=0; i<=9; i++ { for i := 0; i <= 9; i++ {
println(i+2, f() ) println(i+2, f())
} }
} }

View File

@@ -1,37 +1,37 @@
// function_filter2.go // function_filter2.go
package main package main
import "fmt" import "fmt"
type flt func(int) bool type flt func(int) bool
// func isEven(n int) bool { if n%2 == 0 { return true }; return false } // func isEven(n int) bool { if n%2 == 0 { return true }; return false }
func isEven(n int) bool { func isEven(n int) bool {
if n%2 == 0 { if n%2 == 0 {
return true return true
} }
return false return false
} }
func filter(sl []int, f flt) (yes, no []int) { func filter(sl []int, f flt) (yes, no []int) {
for _, val := range sl { for _, val := range sl {
if f(val) { if f(val) {
yes = append(yes, val) yes = append(yes, val)
} else { } else {
no = append(no, val) no = append(no, val)
} }
} }
return return
} }
func main() { func main() {
slice := []int{1, 2, 3, 4, 5, 7} slice := []int{1, 2, 3, 4, 5, 7}
fmt.Println("slice = ", slice) fmt.Println("slice = ", slice)
even, odd := filter(slice, isEven) even, odd := filter(slice, isEven)
fmt.Println("The even elements of slice are: ", even) fmt.Println("The even elements of slice are: ", even)
fmt.Println("The odd elements of slice are: ", odd) fmt.Println("The odd elements of slice are: ", odd)
} }
/* /*
*/ */

View File

@@ -1,18 +1,19 @@
// lambda_value.go // lambda_value.go
package main package main
import ( import (
"fmt" "fmt"
) )
func main() { func main() {
fv := func() { fv := func() {
fmt.Println("Hello World!") fmt.Println("Hello World!")
} }
fv() fv()
fmt.Printf("The type of fv is %T", fv) fmt.Printf("The type of fv is %T", fv)
} }
/* Output:
Hello World! /* Output:
The type of fv is func() Hello World!
*/ The type of fv is func()
*/

View File

@@ -1,24 +1,25 @@
// mult_returnval.go // mult_returnval.go
package main package main
import ( import (
"fmt" "fmt"
) )
func SumProductDiff(i, j int) (int, int, int) { func SumProductDiff(i, j int) (int, int, int) {
return i+j, i*j, i-j return i + j, i * j, i - j
} }
func SumProductDiffN(i, j int) (s int, p int, d int) { func SumProductDiffN(i, j int) (s int, p int, d int) {
s, p, d = i+j, i*j, i-j s, p, d = i+j, i*j, i-j
return return
} }
func main() { func main() {
sum, prod, diff := SumProductDiff(3,4) sum, prod, diff := SumProductDiff(3, 4)
fmt.Println("Sum:", sum, "| Product:",prod, "| Diff:", diff) fmt.Println("Sum:", sum, "| Product:", prod, "| Diff:", diff)
sum, prod, diff = SumProductDiffN(3,4) sum, prod, diff = SumProductDiffN(3, 4)
fmt.Println("Sum:", sum, "| Product:",prod, "| Diff:", diff) fmt.Println("Sum:", sum, "| Product:", prod, "| Diff:", diff)
} }
// Sum: 7 | Product: 12 | Diff: -1
// Sum: 7 | Product: 12 | Diff: -1 // Sum: 7 | Product: 12 | Diff: -1
// Sum: 7 | Product: 12 | Diff: -1

View File

@@ -1,13 +1,19 @@
// strings_map.go // strings_map.go
package main package main
import ( import (
"fmt" "fmt"
"strings" "strings"
) )
func main() { func main() {
asciiOnly := func(c rune) rune { if c > 127 { return ' ' }; return c } asciiOnly := func(c rune) rune {
fmt.Println(strings.Map(asciiOnly, "Jérôme Österreich")) if c > 127 {
} return ' '
// J r me sterreich }
return c
}
fmt.Println(strings.Map(asciiOnly, "Jérôme Österreich"))
}
// J r me sterreich

View File

@@ -1,23 +1,23 @@
// Q10_varargs.go // Q10_varargs.go
package main package main
import ( import (
"fmt" "fmt"
) )
func main() { func main() {
printInts() printInts()
println() println()
printInts(2, 3) printInts(2, 3)
println() println()
printInts(0, 1, 2, 3, 4, 5, 6, 7, 8, 9) printInts(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
} }
func printInts(list ...int) { func printInts(list ...int) {
// for i:=0; i<len(list); i++ { // for i:=0; i<len(list); i++ {
// fmt.Printf("%d\n", list[i]) // fmt.Printf("%d\n", list[i])
// } // }
for _, v := range list { for _, v := range list {
fmt.Printf("%d\n", v) fmt.Printf("%d\n", v)
} }
} }