mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-08-12 07:02:11 +08:00
fix: coding style and file format for chapter 9.
This commit is contained in:
@@ -2,8 +2,8 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"container/list"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -17,6 +17,7 @@ func main() {
|
||||
fmt.Println(e.Value)
|
||||
}
|
||||
}
|
||||
|
||||
/* Example output:
|
||||
&{0x12542bc0 <nil> 0x12547590 1}
|
||||
&{0x12542ba0 0x12542be0 0x12547590 2}
|
||||
|
@@ -1,6 +1,6 @@
|
||||
// even.go
|
||||
package even
|
||||
|
||||
func Even(i int) bool { // exported function
|
||||
func Even(i int) bool { // exported function
|
||||
return i%2 == 0
|
||||
}
|
||||
|
@@ -14,19 +14,21 @@ func Fibonacci(n int) (res int) {
|
||||
func Fibonacci(op string, n int) (res int) {
|
||||
if n <= 1 {
|
||||
switch op {
|
||||
case "+":
|
||||
res = 1
|
||||
case "*":
|
||||
res = 2
|
||||
default: res = 0
|
||||
case "+":
|
||||
res = 1
|
||||
case "*":
|
||||
res = 2
|
||||
default:
|
||||
res = 0
|
||||
}
|
||||
} else {
|
||||
switch op {
|
||||
case "+":
|
||||
res = Fibonacci(op, n-1) + Fibonacci(op, n-2)
|
||||
case "*":
|
||||
res = Fibonacci(op, n-1) * Fibonacci(op, n-2)
|
||||
default: res = 0
|
||||
case "+":
|
||||
res = Fibonacci(op, n-1) + Fibonacci(op, n-2)
|
||||
case "*":
|
||||
res = Fibonacci(op, n-1) * Fibonacci(op, n-2)
|
||||
default:
|
||||
res = 0
|
||||
}
|
||||
}
|
||||
return
|
||||
|
@@ -6,7 +6,7 @@ func GoodDay(name string) string {
|
||||
return "Good Day " + name
|
||||
}
|
||||
|
||||
func GoodNight(name string) string{
|
||||
func GoodNight(name string) string {
|
||||
return "Good Night " + name
|
||||
}
|
||||
|
||||
@@ -24,4 +24,3 @@ func IsEvening() bool {
|
||||
localTime := time.Now()
|
||||
return localTime.Hour() <= 22
|
||||
}
|
||||
|
||||
|
@@ -1,8 +1,8 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"./fibo/fibo"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
var nextFibo int
|
||||
@@ -10,11 +10,11 @@ var op string
|
||||
|
||||
func main() {
|
||||
/*
|
||||
result := 0
|
||||
for i:=0; i <= 10; i++ {
|
||||
result = fibo.Fibonacci(i)
|
||||
fmt.Printf("fibonacci(%d) is: %d\n", i, result)
|
||||
}
|
||||
result := 0
|
||||
for i:=0; i <= 10; i++ {
|
||||
result = fibo.Fibonacci(i)
|
||||
fmt.Printf("fibonacci(%d) is: %d\n", i, result)
|
||||
}
|
||||
*/
|
||||
op = "+"
|
||||
calls()
|
||||
@@ -40,6 +40,7 @@ func next() {
|
||||
result = fibo.Fibonacci(op, nextFibo)
|
||||
fmt.Printf("fibonacci(%d) is: %d\n", nextFibo, result)
|
||||
}
|
||||
|
||||
/* *****************************************************************
|
||||
Output is:
|
||||
fibonacci(1) is: 1
|
||||
@@ -58,5 +59,3 @@ fibonacci(3) is: 8
|
||||
...
|
||||
fibonacci(4) is: 32
|
||||
********************************************************************/
|
||||
|
||||
|
||||
|
@@ -1,8 +1,8 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"./greetings/greetings"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -12,9 +12,9 @@ func main() {
|
||||
|
||||
if greetings.IsAM() {
|
||||
fmt.Println("Good morning", name)
|
||||
} else if greetings.IsAfternoon(){
|
||||
} else if greetings.IsAfternoon() {
|
||||
fmt.Println("Good afternoon", name)
|
||||
} else if greetings.IsEvening(){
|
||||
} else if greetings.IsEvening() {
|
||||
fmt.Println("Good evening", name)
|
||||
} else {
|
||||
fmt.Println("Good night", name)
|
||||
|
@@ -2,12 +2,12 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"./even/even"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func main() {
|
||||
for i:=0; i<=100; i++ {
|
||||
for i := 0; i <= 100; i++ {
|
||||
fmt.Printf("Is the integer %d even? %v\n", i, even.Even(i))
|
||||
}
|
||||
}
|
||||
|
@@ -11,4 +11,5 @@ func main() {
|
||||
size := unsafe.Sizeof(i)
|
||||
fmt.Println("The size of an int is: ", size)
|
||||
}
|
||||
|
||||
// The size of an int is: 4
|
||||
|
Reference in New Issue
Block a user