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.
fix: coding style and file format for chapter 9.
- Loading branch information
Showing
8 changed files
with
207 additions
and
205 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,28 +1,29 @@ | ||
// Q20_linked_list.go | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"container/list" | ||
) | ||
|
||
func main() { | ||
lst := list.New() | ||
lst.PushBack(100) | ||
lst.PushBack(101) | ||
lst.PushBack(102) | ||
// fmt.Println("Here is the double linked list:\n", lst) | ||
for e := lst.Front(); e != nil; e = e.Next() { | ||
// fmt.Println(e) | ||
fmt.Println(e.Value) | ||
} | ||
} | ||
/* Example output: | ||
&{0x12542bc0 <nil> 0x12547590 1} | ||
&{0x12542ba0 0x12542be0 0x12547590 2} | ||
&{<nil> 0x12542bc0 0x12547590 4} | ||
100 | ||
101 | ||
102 | ||
*/ | ||
// Q20_linked_list.go | ||
package main | ||
|
||
import ( | ||
"container/list" | ||
"fmt" | ||
) | ||
|
||
func main() { | ||
lst := list.New() | ||
lst.PushBack(100) | ||
lst.PushBack(101) | ||
lst.PushBack(102) | ||
// fmt.Println("Here is the double linked list:\n", lst) | ||
for e := lst.Front(); e != nil; e = e.Next() { | ||
// fmt.Println(e) | ||
fmt.Println(e.Value) | ||
} | ||
} | ||
|
||
/* Example output: | ||
&{0x12542bc0 <nil> 0x12547590 1} | ||
&{0x12542ba0 0x12542be0 0x12547590 2} | ||
&{<nil> 0x12542bc0 0x12547590 4} | ||
100 | ||
101 | ||
102 | ||
*/ |
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,6 +1,6 @@ | ||
// even.go | ||
package even | ||
|
||
func Even(i int) bool { // exported function | ||
return i%2 == 0 | ||
} | ||
// even.go | ||
package even | ||
|
||
func Even(i int) bool { // exported function | ||
return i%2 == 0 | ||
} |
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,33 +1,35 @@ | ||
package fibo | ||
|
||
/* | ||
func Fibonacci(n int) (res int) { | ||
if n <= 1 { | ||
res = 1 | ||
} else { | ||
res = Fibonacci(n-1) + Fibonacci(n-2) | ||
} | ||
return | ||
} | ||
*/ | ||
// accepts a general operation op: | ||
func Fibonacci(op string, n int) (res int) { | ||
if n <= 1 { | ||
switch op { | ||
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 | ||
} | ||
} | ||
return | ||
} | ||
package fibo | ||
|
||
/* | ||
func Fibonacci(n int) (res int) { | ||
if n <= 1 { | ||
res = 1 | ||
} else { | ||
res = Fibonacci(n-1) + Fibonacci(n-2) | ||
} | ||
return | ||
} | ||
*/ | ||
// accepts a general operation op: | ||
func Fibonacci(op string, n int) (res int) { | ||
if n <= 1 { | ||
switch op { | ||
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 | ||
} | ||
} | ||
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,27 +1,26 @@ | ||
package greetings | ||
|
||
import "time" | ||
|
||
func GoodDay(name string) string { | ||
return "Good Day " + name | ||
} | ||
|
||
func GoodNight(name string) string{ | ||
return "Good Night " + name | ||
} | ||
|
||
func IsAM() bool { | ||
localTime := time.Now() | ||
return localTime.Hour() <= 12 | ||
} | ||
|
||
func IsAfternoon() bool { | ||
localTime := time.Now() | ||
return localTime.Hour() <= 18 | ||
} | ||
|
||
func IsEvening() bool { | ||
localTime := time.Now() | ||
return localTime.Hour() <= 22 | ||
} | ||
|
||
package greetings | ||
|
||
import "time" | ||
|
||
func GoodDay(name string) string { | ||
return "Good Day " + name | ||
} | ||
|
||
func GoodNight(name string) string { | ||
return "Good Night " + name | ||
} | ||
|
||
func IsAM() bool { | ||
localTime := time.Now() | ||
return localTime.Hour() <= 12 | ||
} | ||
|
||
func IsAfternoon() bool { | ||
localTime := time.Now() | ||
return localTime.Hour() <= 18 | ||
} | ||
|
||
func IsEvening() bool { | ||
localTime := time.Now() | ||
return localTime.Hour() <= 22 | ||
} |
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,62 +1,61 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"./fibo/fibo" | ||
) | ||
|
||
var nextFibo int | ||
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) | ||
} | ||
*/ | ||
op = "+" | ||
calls() | ||
fmt.Println("Change of operation from + to *") | ||
nextFibo = 0 | ||
op = "*" | ||
calls() | ||
} | ||
|
||
func calls() { | ||
next() | ||
fmt.Println("...") | ||
next() | ||
fmt.Println("...") | ||
next() | ||
fmt.Println("...") | ||
next() | ||
} | ||
|
||
func next() { | ||
result := 0 | ||
nextFibo++ | ||
result = fibo.Fibonacci(op, nextFibo) | ||
fmt.Printf("fibonacci(%d) is: %d\n", nextFibo, result) | ||
} | ||
/* ***************************************************************** | ||
Output is: | ||
fibonacci(1) is: 1 | ||
... | ||
fibonacci(2) is: 2 | ||
... | ||
fibonacci(3) is: 3 | ||
... | ||
fibonacci(4) is: 5 | ||
Change of operation from + to * | ||
fibonacci(1) is: 2 | ||
... | ||
fibonacci(2) is: 4 | ||
... | ||
fibonacci(3) is: 8 | ||
... | ||
fibonacci(4) is: 32 | ||
********************************************************************/ | ||
|
||
|
||
package main | ||
|
||
import ( | ||
"./fibo/fibo" | ||
"fmt" | ||
) | ||
|
||
var nextFibo int | ||
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) | ||
} | ||
*/ | ||
op = "+" | ||
calls() | ||
fmt.Println("Change of operation from + to *") | ||
nextFibo = 0 | ||
op = "*" | ||
calls() | ||
} | ||
|
||
func calls() { | ||
next() | ||
fmt.Println("...") | ||
next() | ||
fmt.Println("...") | ||
next() | ||
fmt.Println("...") | ||
next() | ||
} | ||
|
||
func next() { | ||
result := 0 | ||
nextFibo++ | ||
result = fibo.Fibonacci(op, nextFibo) | ||
fmt.Printf("fibonacci(%d) is: %d\n", nextFibo, result) | ||
} | ||
|
||
/* ***************************************************************** | ||
Output is: | ||
fibonacci(1) is: 1 | ||
... | ||
fibonacci(2) is: 2 | ||
... | ||
fibonacci(3) is: 3 | ||
... | ||
fibonacci(4) is: 5 | ||
Change of operation from + to * | ||
fibonacci(1) is: 2 | ||
... | ||
fibonacci(2) is: 4 | ||
... | ||
fibonacci(3) is: 8 | ||
... | ||
fibonacci(4) is: 32 | ||
********************************************************************/ |
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,22 +1,22 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"./greetings/greetings" | ||
) | ||
|
||
func main() { | ||
name := "James" | ||
fmt.Println(greetings.GoodDay(name)) | ||
fmt.Println(greetings.GoodNight(name)) | ||
if greetings.IsAM() { | ||
fmt.Println("Good morning", name) | ||
} else if greetings.IsAfternoon(){ | ||
fmt.Println("Good afternoon", name) | ||
} else if greetings.IsEvening(){ | ||
fmt.Println("Good evening", name) | ||
} else { | ||
fmt.Println("Good night", name) | ||
} | ||
} | ||
package main | ||
|
||
import ( | ||
"./greetings/greetings" | ||
"fmt" | ||
) | ||
|
||
func main() { | ||
name := "James" | ||
fmt.Println(greetings.GoodDay(name)) | ||
fmt.Println(greetings.GoodNight(name)) | ||
|
||
if greetings.IsAM() { | ||
fmt.Println("Good morning", name) | ||
} else if greetings.IsAfternoon() { | ||
fmt.Println("Good afternoon", name) | ||
} else if greetings.IsEvening() { | ||
fmt.Println("Good evening", name) | ||
} else { | ||
fmt.Println("Good night", name) | ||
} | ||
} |
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,13 @@ | ||
// test_oddeven.go | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"./even/even" | ||
) | ||
|
||
func main() { | ||
for i:=0; i<=100; i++ { | ||
fmt.Printf("Is the integer %d even? %v\n", i, even.Even(i)) | ||
} | ||
} | ||
// test_oddeven.go | ||
package main | ||
|
||
import ( | ||
"./even/even" | ||
"fmt" | ||
) | ||
|
||
func main() { | ||
for i := 0; i <= 100; i++ { | ||
fmt.Printf("Is the integer %d even? %v\n", i, even.Even(i)) | ||
} | ||
} |
Oops, something went wrong.