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

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

View File

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

View File

@@ -15,9 +15,9 @@ func fibonacci(n int) (val, pos int) {
if n <= 1 {
val = 1
} else {
v1, _ := fibonacci(n-1)
v2, _ := fibonacci(n-2)
val = v1 + v2
v1, _ := fibonacci(n - 1)
v2, _ := fibonacci(n - 2)
val = v1 + v2
}
pos = n
return

View File

@@ -14,7 +14,7 @@ func main() {
f := fib()
// Function calls are evaluated left-to-right.
// println(f(), f(), f(), f(), f())
for i:=0; i<=9; i++ {
println(i+2, f() )
for i := 0; i <= 9; i++ {
println(i+2, f())
}
}

View File

@@ -34,4 +34,4 @@ func main() {
/*
*/
*/

View File

@@ -12,6 +12,7 @@ func main() {
fv()
fmt.Printf("The type of fv is %T", fv)
}
/* Output:
Hello World!
The type of fv is func()

View File

@@ -2,23 +2,24 @@
package main
import (
"fmt"
"fmt"
)
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) {
s, p, d = i+j, i*j, i-j
return
return
}
func main() {
sum, prod, diff := SumProductDiff(3,4)
fmt.Println("Sum:", sum, "| Product:",prod, "| Diff:", diff)
sum, prod, diff = SumProductDiffN(3,4)
fmt.Println("Sum:", sum, "| Product:",prod, "| Diff:", diff)
sum, prod, diff := SumProductDiff(3, 4)
fmt.Println("Sum:", sum, "| Product:", prod, "| Diff:", diff)
sum, prod, diff = SumProductDiffN(3, 4)
fmt.Println("Sum:", sum, "| Product:", prod, "| Diff:", diff)
}
// Sum: 7 | Product: 12 | Diff: -1
// Sum: 7 | Product: 12 | Diff: -1

View File

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

View File

@@ -14,9 +14,9 @@ func main() {
}
func printInts(list ...int) {
// for i:=0; i<len(list); i++ {
// fmt.Printf("%d\n", list[i])
// }
// for i:=0; i<len(list); i++ {
// fmt.Printf("%d\n", list[i])
// }
for _, v := range list {
fmt.Printf("%d\n", v)
}