Merge pull request #316 from appleboy/patch-2

fix: coding style and file format for chapter 5
This commit is contained in:
无闻
2017-02-10 23:23:57 -05:00
committed by GitHub
8 changed files with 162 additions and 153 deletions

View File

@@ -3,10 +3,11 @@ package main
import "fmt" import "fmt"
func main() { func main() {
for i:=0; i <= 10; i ++ { for i := 0; i <= 10; i++ {
fmt.Printf("the complement of %b is: %b\n", i, ^i) fmt.Printf("the complement of %b is: %b\n", i, ^i)
} }
} }
/* Output: /* Output:
the complement of 0 is: -1 the complement of 0 is: -1
the complement of 1 is: -10 the complement of 1 is: -10

View File

@@ -3,22 +3,22 @@ package main
import "fmt" import "fmt"
const ( const (
FIZZ=3 FIZZ = 3
BUZZ=5 BUZZ = 5
FIZZBUZZ=15 FIZZBUZZ = 15
) )
func main() { func main() {
for i:=0; i <= 100; i++ { for i := 0; i <= 100; i++ {
switch { switch {
case i%FIZZBUZZ==0: case i%FIZZBUZZ == 0:
fmt.Println("FizzBuzz") fmt.Println("FizzBuzz")
case i%FIZZ==0: case i%FIZZ == 0:
fmt.Println("Fizz") fmt.Println("Fizz")
case i%BUZZ==0: case i%BUZZ == 0:
fmt.Println("Buzz") fmt.Println("Buzz")
default: default:
fmt.Println(i) fmt.Println(i)
} }
} }
} }

View File

@@ -2,15 +2,15 @@ package main
func main() { func main() {
// 1 - use 2 nested for loops // 1 - use 2 nested for loops
for i:=1; i <= 25; i++ { for i := 1; i <= 25; i++ {
for j:=1; j <=i; j++ { for j := 1; j <= i; j++ {
print("G") print("G")
} }
println() println()
} }
// 2 - use only one for loop and string concatenation // 2 - use only one for loop and string concatenation
str := "G" str := "G"
for i:=1; i <= 25; i++ { for i := 1; i <= 25; i++ {
println(str) println(str)
str += "G" str += "G"
} }

View File

@@ -4,7 +4,7 @@ import "fmt"
func main() { func main() {
// 1: // 1:
for i:=0; i < 15; i++ { for i := 0; i < 15; i++ {
fmt.Printf("The counter is at %d\n", i) fmt.Printf("The counter is at %d\n", i)
} }
// 2: // 2:
@@ -12,5 +12,7 @@ func main() {
START: START:
fmt.Printf("The counter is at %d\n", i) fmt.Printf("The counter is at %d\n", i)
i++ i++
if i < 15 { goto START } if i < 15 {
goto START
}
} }

View File

@@ -7,8 +7,8 @@ import (
func main() { func main() {
var i int var i int
for i=0; i<10; i++ { for i = 0; i < 10; i++ {
fmt.Printf("%v\n", i) fmt.Printf("%v\n", i)
} }
fmt.Printf("%v\n", i) //<-- compile error: undefined i fmt.Printf("%v\n", i) //<-- compile error: undefined i
} }

View File

@@ -4,12 +4,13 @@ package main
import "fmt" import "fmt"
func main() { func main() {
//multiple initialization; a consolidated bool expression with && and ||; multiple incrementation //multiple initialization; a consolidated bool expression with && and ||; multiple incrementation
for i, j, s := 0, 5, "a"; i < 3 && j < 100 && s != "aaaaa"; i, j, s = i+1, for i, j, s := 0, 5, "a"; i < 3 && j < 100 && s != "aaaaa"; i, j, s = i+1,
j+1, s + "a" { j+1, s+"a" {
fmt.Println("Value of i, j, s:", i, j, s) fmt.Println("Value of i, j, s:", i, j, s)
} }
} }
/* Output: /* Output:
Value of i, j, s: 0 5 a Value of i, j, s: 0 5 a
Value of i, j, s: 1 6 aa Value of i, j, s: 1 6 aa

View File

@@ -12,6 +12,7 @@ func main() {
fmt.Println() fmt.Println()
} }
} }
/* Output: /* Output:
******************** ********************
******************** ********************
@@ -23,4 +24,4 @@ func main() {
******************** ********************
******************** ********************
******************** ********************
*/ */

View File

@@ -8,10 +8,14 @@ func main() {
func Season(month int) string { func Season(month int) string {
switch month { switch month {
case 12,1,2: return "Winter" case 12, 1, 2:
case 3,4,5: return "Spring" return "Winter"
case 6,7,8: return "Summer" case 3, 4, 5:
case 9,10,11: return "Autumn" return "Spring"
case 6, 7, 8:
return "Summer"
case 9, 10, 11:
return "Autumn"
} }
return "Season unknown" return "Season unknown"
} }