fix: coding style and file format for chapter 8.

This commit is contained in:
Bo-Yi Wu
2017-02-11 12:27:17 +08:00
parent d9041c7fc3
commit 6edacddbb3
2 changed files with 110 additions and 108 deletions

View File

@@ -1,35 +1,36 @@
// map_days.go // map_days.go
package main package main
import ( import (
"fmt" "fmt"
) )
var Days = map[int]string{1:"monday", var Days = map[int]string{1: "monday",
2:"tuesday", 2: "tuesday",
3: "wednesday", 3: "wednesday",
4: "thursday", 4: "thursday",
5: "friday", 5: "friday",
6: "saturday", 6: "saturday",
7: "sunday"} 7: "sunday"}
func main() { func main() {
fmt.Println(Days) fmt.Println(Days)
// fmt.Printf("%v", Days) // fmt.Printf("%v", Days)
flagHolliday := false flagHolliday := false
for k, v := range Days { for k, v := range Days {
if v == "thursday" || v == "holliday" { if v == "thursday" || v == "holliday" {
fmt.Println(v, " is the ", k , "th day in the week") fmt.Println(v, " is the ", k, "th day in the week")
if v == "holliday" { if v == "holliday" {
flagHolliday = true flagHolliday = true
} }
} }
} }
if !flagHolliday { if !flagHolliday {
fmt.Println("holliday is not a day!") fmt.Println("holliday is not a day!")
} }
} }
/* Output:
thursday is the 4 th day in the week /* Output:
holliday is not a day! thursday is the 4 th day in the week
*/ holliday is not a day!
*/

View File

@@ -1,73 +1,74 @@
// map_drinks.go // map_drinks.go
package main package main
import ( import (
"fmt" "fmt"
"sort" "sort"
) )
func main() { func main() {
drinks := map[string]string{ drinks := map[string]string{
"beer": "bière", "beer": "bière",
"wine": "vin", "wine": "vin",
"water": "eau", "water": "eau",
"coffee": "café", "coffee": "café",
"thea": "thé"} "thea": "thé"}
sdrinks := make([]string, len(drinks)) sdrinks := make([]string, len(drinks))
ix := 0 ix := 0
fmt.Printf("The following drinks are available:\n") fmt.Printf("The following drinks are available:\n")
for eng := range drinks { for eng := range drinks {
sdrinks[ix] = eng sdrinks[ix] = eng
ix++ ix++
fmt.Println(eng) fmt.Println(eng)
} }
fmt.Println("") fmt.Println("")
for eng, fr := range drinks { for eng, fr := range drinks {
fmt.Printf("The french for %s is %s\n", eng, fr) fmt.Printf("The french for %s is %s\n", eng, fr)
} }
// SORTING: // SORTING:
fmt.Println("") fmt.Println("")
fmt.Println("Now the sorted output:") fmt.Println("Now the sorted output:")
sort.Strings(sdrinks) sort.Strings(sdrinks)
fmt.Printf("The following sorted drinks are available:\n") fmt.Printf("The following sorted drinks are available:\n")
for _, eng := range sdrinks { for _, eng := range sdrinks {
fmt.Println(eng) fmt.Println(eng)
} }
fmt.Println("") fmt.Println("")
for _, eng := range sdrinks { for _, eng := range sdrinks {
fmt.Printf("The french for %s is %s\n", eng, drinks[eng]) fmt.Printf("The french for %s is %s\n", eng, drinks[eng])
} }
} }
/* Output:
The following drinks are available: /* Output:
wine The following drinks are available:
beer wine
water beer
coffee water
thea coffee
thea
The french for wine is vin
The french for beer is bière The french for wine is vin
The french for water is eau The french for beer is bière
The french for coffee is café The french for water is eau
The french for thea is thé The french for coffee is café
The french for thea is thé
Now the sorted output:
The following sorted drinks are available: Now the sorted output:
beer The following sorted drinks are available:
coffee beer
thea coffee
water thea
wine water
wine
The french for beer is bière
The french for coffee is café The french for beer is bière
The french for thea is thé The french for coffee is café
The french for water is eau The french for thea is thé
The french for wine is vin The french for water is eau
*/ The french for wine is vin
*/