mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-08-11 23:08:34 +08:00
add 08.2.md 08.3.md 08.4.md 08.5.md 08.6.md
This commit is contained in:
23
eBook/examples/chapter_8/invert_map.go
Normal file
23
eBook/examples/chapter_8/invert_map.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package main
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
var (
|
||||
barVal = map[string]int{"alpha": 34, "bravo": 56, "charlie": 23,
|
||||
"delta": 87, "echo": 56, "foxtrot": 12,
|
||||
"golf": 34, "hotel": 16, "indio": 87,
|
||||
"juliet": 65, "kili": 43, "lima": 98}
|
||||
)
|
||||
|
||||
func main() {
|
||||
invMap := make(map[int]string, len(barVal))
|
||||
for k, v := range barVal {
|
||||
invMap[v] = k
|
||||
}
|
||||
fmt.Println("inverted:")
|
||||
for k, v := range invMap {
|
||||
fmt.Printf("Key: %v, Value: %v / ", k, v)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
31
eBook/examples/chapter_8/map_testelement.go
Normal file
31
eBook/examples/chapter_8/map_testelement.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package main
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
var value int
|
||||
var isPresent bool
|
||||
|
||||
map1 := make(map[string]int)
|
||||
map1["New Delhi"] = 55
|
||||
map1["Beijing"] = 20
|
||||
map1["Washington"] = 25
|
||||
value, isPresent = map1["Beijing"]
|
||||
if isPresent {
|
||||
fmt.Printf("The value of \"Beijin\" in map1 is: %d\n", value)
|
||||
} else {
|
||||
fmt.Printf("map1 does not contain Beijing")
|
||||
}
|
||||
|
||||
value, isPresent = map1["Paris"]
|
||||
fmt.Printf("Is \"Paris\" in map1 ?: %t\n", isPresent)
|
||||
fmt.Printf("Value is: %d\n", value)
|
||||
|
||||
// delete an item:
|
||||
delete(map1, "Washington")
|
||||
value, isPresent = map1["Washington"]
|
||||
if isPresent {
|
||||
fmt.Printf("The value of \"Washington\" in map1 is: %d\n", value)
|
||||
} else {
|
||||
fmt.Println("map1 does not contain Washington")
|
||||
}
|
||||
}
|
13
eBook/examples/chapter_8/maps_forrange.go
Normal file
13
eBook/examples/chapter_8/maps_forrange.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package main
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
map1 := make(map[int]float32)
|
||||
map1[1] = 1.0
|
||||
map1[2] = 2.0
|
||||
map1[3] = 3.0
|
||||
map1[4] = 4.0
|
||||
for key, value := range map1 {
|
||||
fmt.Printf("key is: %d - value is: %f\n", key, value)
|
||||
}
|
||||
}
|
20
eBook/examples/chapter_8/slice_maps.go
Normal file
20
eBook/examples/chapter_8/slice_maps.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package main
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
// Version A:
|
||||
items := make([]map[int]int, 5)
|
||||
for i:= range items {
|
||||
items[i] = make(map[int]int, 1)
|
||||
items[i][1] = 2
|
||||
}
|
||||
fmt.Printf("Version A: Value of items: %v\n", items)
|
||||
|
||||
// Version B: NOT GOOD!
|
||||
items2 := make([]map[int]int, 5)
|
||||
for _, item := range items2 {
|
||||
item = make(map[int]int, 1) // item is only a copy of the slice element.
|
||||
item[1] = 2 // This 'item' will be lost on the next iteration.
|
||||
}
|
||||
fmt.Printf("Version B: Value of items: %v\n", items2)
|
||||
}
|
32
eBook/examples/chapter_8/sort_map.go
Normal file
32
eBook/examples/chapter_8/sort_map.go
Normal file
@@ -0,0 +1,32 @@
|
||||
// the telephone alphabet:
|
||||
package main
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
)
|
||||
|
||||
var (
|
||||
barVal = map[string]int{"alpha": 34, "bravo": 56, "charlie": 23,
|
||||
"delta": 87, "echo": 56, "foxtrot": 12,
|
||||
"golf": 34, "hotel": 16, "indio": 87,
|
||||
"juliet": 65, "kili": 43, "lima": 98}
|
||||
)
|
||||
|
||||
func main() {
|
||||
fmt.Println("unsorted:")
|
||||
for k, v := range barVal {
|
||||
fmt.Printf("Key: %v, Value: %v / ", k, v)
|
||||
}
|
||||
keys := make([]string, len(barVal))
|
||||
i := 0
|
||||
for k, _ := range barVal {
|
||||
keys[i] = k
|
||||
i++
|
||||
}
|
||||
sort.Strings(keys)
|
||||
fmt.Println()
|
||||
fmt.Println("sorted:")
|
||||
for _, k := range keys {
|
||||
fmt.Printf("Key: %v, Value: %v / ", k, barVal[k])
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user