forked from unknwon/the-way-to-go_ZH_CN
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request unknwon#317 from appleboy/patch-3
fix: coding style and file format for chapter 6.
- Loading branch information
Showing
11 changed files
with
290 additions
and
279 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,15 @@ | ||
package main | ||
|
||
import "fmt" | ||
|
||
func main() { | ||
printrec(1) | ||
} | ||
|
||
func printrec(i int) { | ||
if i>10 { return } | ||
printrec(i+1) | ||
fmt.Printf("%d ", i) | ||
} | ||
package main | ||
|
||
import "fmt" | ||
|
||
func main() { | ||
printrec(1) | ||
} | ||
|
||
func printrec(i int) { | ||
if i > 10 { | ||
return | ||
} | ||
printrec(i + 1) | ||
fmt.Printf("%d ", i) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,17 @@ | ||
// compose.go | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"math" | ||
) | ||
|
||
func Compose(f, g func(x float64) float64) func(x float64) float64 { | ||
return func(x float64) float64 { // closure | ||
return f(g(x)) | ||
} | ||
} | ||
|
||
func main() { | ||
fmt.Print(Compose(math.Sin, math.Cos)(0.5)) // output: 0.7691963548410085 | ||
} | ||
// compose.go | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"math" | ||
) | ||
|
||
func Compose(f, g func(x float64) float64) func(x float64) float64 { | ||
return func(x float64) float64 { // closure | ||
return f(g(x)) | ||
} | ||
} | ||
|
||
func main() { | ||
fmt.Print(Compose(math.Sin, math.Cos)(0.5)) // output: 0.7691963548410085 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,57 @@ | ||
// error_returnval.go | ||
package main | ||
|
||
import ( | ||
"errors" | ||
"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("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 | ||
} | ||
|
||
//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 | ||
} | ||
/* 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> | ||
2.23606797749979 <nil> | ||
*/ | ||
// error_returnval.go | ||
package main | ||
|
||
import ( | ||
"errors" | ||
"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("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 | ||
} | ||
|
||
//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 | ||
} | ||
|
||
/* 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> | ||
2.23606797749979 <nil> | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,34 @@ | ||
// factorial.go | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
func main() { | ||
for i := uint64(0); i < uint64(30); i++ { | ||
fmt.Printf("Factorial of %d is %d\n", i, Factorial(i)) | ||
} | ||
} | ||
|
||
/* unnamed return variables: | ||
func Factorial(n uint64) uint64 { | ||
if n > 0 { | ||
return n * Factorial(n-1) | ||
} | ||
return 1 | ||
} | ||
*/ | ||
|
||
// named return variables: | ||
func Factorial(n uint64) (fac uint64) { | ||
fac = 1 | ||
if n > 0 { | ||
fac = n * Factorial(n-1) | ||
return | ||
} | ||
return | ||
} | ||
|
||
// int: correct results till 12! | ||
// uint64: correct results till 21! | ||
// factorial.go | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
func main() { | ||
for i := uint64(0); i < uint64(30); i++ { | ||
fmt.Printf("Factorial of %d is %d\n", i, Factorial(i)) | ||
} | ||
} | ||
|
||
/* unnamed return variables: | ||
func Factorial(n uint64) uint64 { | ||
if n > 0 { | ||
return n * Factorial(n-1) | ||
} | ||
return 1 | ||
} | ||
*/ | ||
|
||
// named return variables: | ||
func Factorial(n uint64) (fac uint64) { | ||
fac = 1 | ||
if n > 0 { | ||
fac = n * Factorial(n-1) | ||
return | ||
} | ||
return | ||
} | ||
|
||
// int: correct results till 12! | ||
// uint64: correct results till 21! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,24 @@ | ||
package main | ||
|
||
import "fmt" | ||
|
||
func main() { | ||
pos := 4 | ||
result, pos := fibonacci(pos) | ||
fmt.Printf("the %d-th fibonacci number is: %d\n", pos, result) | ||
pos = 10 | ||
result, pos = fibonacci(pos) | ||
fmt.Printf("the %d-th fibonacci number is: %d\n", pos, result) | ||
} | ||
|
||
func fibonacci(n int) (val, pos int) { | ||
if n <= 1 { | ||
val = 1 | ||
} else { | ||
v1, _ := fibonacci(n-1) | ||
v2, _ := fibonacci(n-2) | ||
val = v1 + v2 | ||
} | ||
pos = n | ||
return | ||
} | ||
package main | ||
|
||
import "fmt" | ||
|
||
func main() { | ||
pos := 4 | ||
result, pos := fibonacci(pos) | ||
fmt.Printf("the %d-th fibonacci number is: %d\n", pos, result) | ||
pos = 10 | ||
result, pos = fibonacci(pos) | ||
fmt.Printf("the %d-th fibonacci number is: %d\n", pos, result) | ||
} | ||
|
||
func fibonacci(n int) (val, pos int) { | ||
if n <= 1 { | ||
val = 1 | ||
} else { | ||
v1, _ := fibonacci(n - 1) | ||
v2, _ := fibonacci(n - 2) | ||
val = v1 + v2 | ||
} | ||
pos = n | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,20 @@ | ||
package main | ||
|
||
// fib returns a function that returns | ||
// successive Fibonacci numbers. | ||
func fib() func() int { | ||
a, b := 1, 1 | ||
return func() int { | ||
a, b = b, a+b | ||
return b | ||
} | ||
} | ||
|
||
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() ) | ||
} | ||
} | ||
package main | ||
|
||
// fib returns a function that returns | ||
// successive Fibonacci numbers. | ||
func fib() func() int { | ||
a, b := 1, 1 | ||
return func() int { | ||
a, b = b, a+b | ||
return b | ||
} | ||
} | ||
|
||
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()) | ||
} | ||
} |
Oops, something went wrong.