mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-08-12 05:33:04 +08:00
Merge pull request #317 from appleboy/patch-3
fix: coding style and file format for chapter 6.
This commit is contained in:
@@ -7,7 +7,9 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func printrec(i int) {
|
func printrec(i int) {
|
||||||
if i>10 { return }
|
if i > 10 {
|
||||||
|
return
|
||||||
|
}
|
||||||
printrec(i + 1)
|
printrec(i + 1)
|
||||||
fmt.Printf("%d ", i)
|
fmt.Printf("%d ", i)
|
||||||
}
|
}
|
@@ -29,7 +29,7 @@ func main() {
|
|||||||
|
|
||||||
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
|
||||||
@@ -38,7 +38,7 @@ func MySqrt(f float64) (float64, error) {
|
|||||||
|
|
||||||
//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!")
|
||||||
@@ -49,6 +49,7 @@ func MySqrt2(f float64) (ret float64, err error) {
|
|||||||
//automatically return the named return variables ret and err
|
//automatically return the named return variables ret and err
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Output:
|
/* Output:
|
||||||
First example with -1: Error! Return values are: NaN I won't be able to do a sqrt of negative number!
|
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>
|
Second example with 5: It's ok! Return values are: 2.23606797749979 <nil>
|
||||||
|
@@ -12,6 +12,7 @@ func main() {
|
|||||||
fv()
|
fv()
|
||||||
fmt.Printf("The type of fv is %T", fv)
|
fmt.Printf("The type of fv is %T", fv)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Output:
|
/* Output:
|
||||||
Hello World!
|
Hello World!
|
||||||
The type of fv is func()
|
The type of fv is func()
|
||||||
|
@@ -20,5 +20,6 @@ func main() {
|
|||||||
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
|
@@ -7,7 +7,13 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
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"))
|
fmt.Println(strings.Map(asciiOnly, "Jérôme Österreich"))
|
||||||
}
|
}
|
||||||
|
|
||||||
// J r me sterreich
|
// J r me sterreich
|
Reference in New Issue
Block a user