mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-08-12 05:11:49 +08:00
fix: coding style and file format for all example.
This commit is contained in:
@@ -1,29 +1,29 @@
|
||||
// invert_map.go
|
||||
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, "kilo": 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)
|
||||
}
|
||||
}
|
||||
/* Output:
|
||||
inverted:
|
||||
Key: 12, Value: foxtrot / Key: 16, Value: hotel / Key: 87, Value: delta / Key: 23, Value: charlie /
|
||||
Key: 65, Value: juliet / Key: 43, Value: kilo / Key: 56, Value: bravo / Key: 98, Value: lima /
|
||||
Key: 34, Value: golf /
|
||||
*/
|
||||
|
||||
// invert_map.go
|
||||
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, "kilo": 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)
|
||||
}
|
||||
}
|
||||
|
||||
/* Output:
|
||||
inverted:
|
||||
Key: 12, Value: foxtrot / Key: 16, Value: hotel / Key: 87, Value: delta / Key: 23, Value: charlie /
|
||||
Key: 65, Value: juliet / Key: 43, Value: kilo / Key: 56, Value: bravo / Key: 98, Value: lima /
|
||||
Key: 34, Value: golf /
|
||||
*/
|
||||
|
@@ -20,6 +20,7 @@ func main() {
|
||||
fmt.Printf("Map assigned at \"two\" is: %d\n", mapLit["two"])
|
||||
fmt.Printf("Map literal at \"ten\" is: %d\n", mapLit["ten"])
|
||||
}
|
||||
|
||||
/* Output:
|
||||
Map literal at "one" is: 1
|
||||
Map created at "key2" is: 3.141590
|
||||
|
@@ -1,13 +1,15 @@
|
||||
// map_func.go
|
||||
package main
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
mf := map[int]func() int{
|
||||
1: func() int { return 10 },
|
||||
2: func() int { return 20 },
|
||||
5: func() int { return 50 },
|
||||
}
|
||||
fmt.Println(mf)
|
||||
}
|
||||
// Output: map[1:0x10903be0 5:0x10903ba0 2:0x10903bc0]
|
||||
// map_func.go
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
mf := map[int]func() int{
|
||||
1: func() int { return 10 },
|
||||
2: func() int { return 20 },
|
||||
5: func() int { return 50 },
|
||||
}
|
||||
fmt.Println(mf)
|
||||
}
|
||||
|
||||
// Output: map[1:0x10903be0 5:0x10903ba0 2:0x10903bc0]
|
||||
|
@@ -1,33 +1,33 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
var value int
|
||||
var isPresent bool
|
||||
|
||||
map1 := make(map[string]int)
|
||||
map1["New Delhi"] = 55
|
||||
map1["Bejing"] = 20
|
||||
map1["Washington"] = 25
|
||||
|
||||
value, isPresent = map1["Bejing"]
|
||||
if isPresent {
|
||||
fmt.Printf("The value of \"Bejing\" in map1 is: %d\n", value)
|
||||
} else {
|
||||
fmt.Println("map1 does not contain Bejing")
|
||||
}
|
||||
|
||||
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")
|
||||
}
|
||||
}
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
var value int
|
||||
var isPresent bool
|
||||
|
||||
map1 := make(map[string]int)
|
||||
map1["New Delhi"] = 55
|
||||
map1["Bejing"] = 20
|
||||
map1["Washington"] = 25
|
||||
|
||||
value, isPresent = map1["Bejing"]
|
||||
if isPresent {
|
||||
fmt.Printf("The value of \"Bejing\" in map1 is: %d\n", value)
|
||||
} else {
|
||||
fmt.Println("map1 does not contain Bejing")
|
||||
}
|
||||
|
||||
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")
|
||||
}
|
||||
}
|
||||
|
@@ -1,15 +1,15 @@
|
||||
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)
|
||||
}
|
||||
}
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
@@ -1,27 +1,28 @@
|
||||
// slice_maps.go
|
||||
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:
|
||||
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)
|
||||
}
|
||||
/* Output:
|
||||
Version A: Value of items: [map[1:2] map[1:2] map[1:2] map[1:2] map[1:2]]
|
||||
Version B: Value of items: [map[] map[] map[] map[] map[]]
|
||||
*/
|
||||
// slice_maps.go
|
||||
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:
|
||||
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)
|
||||
}
|
||||
|
||||
/* Output:
|
||||
Version A: Value of items: [map[1:2] map[1:2] map[1:2] map[1:2] map[1:2]]
|
||||
Version B: Value of items: [map[] map[] map[] map[] map[]]
|
||||
*/
|
||||
|
@@ -1,43 +1,43 @@
|
||||
// sort_map.go
|
||||
// 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, "kilo": 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])
|
||||
}
|
||||
}
|
||||
/* Output:
|
||||
unsorted:
|
||||
Key: indio, Value: 87 / Key: echo, Value: 56 / Key: juliet, Value: 65 / Key: charlie, Value: 23 /
|
||||
Key: hotel, Value: 16 / Key: lima, Value: 98 / Key: bravo, Value: 56 / Key: alpha, Value: 34 /
|
||||
Key: kilo, Value: 43 / Key: delta, Value: 87 / Key: golf, Value: 34 / Key: foxtrot, Value: 12 /
|
||||
sorted:
|
||||
Key: alpha, Value: 34 / Key: bravo, Value: 56 / Key: charlie, Value: 23 / Key: delta, Value: 87 /
|
||||
Key: echo, Value: 56 / Key: foxtrot, Value: 12 / Key: golf, Value: 34 / Key: hotel, Value: 16 /
|
||||
Key: indio, Value: 87 / Key: juliet, Value: 65 / Key: kilo, Value: 43 / Key: lima, Value: 98 /
|
||||
*/
|
||||
|
||||
// sort_map.go
|
||||
// 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, "kilo": 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])
|
||||
}
|
||||
}
|
||||
|
||||
/* Output:
|
||||
unsorted:
|
||||
Key: indio, Value: 87 / Key: echo, Value: 56 / Key: juliet, Value: 65 / Key: charlie, Value: 23 /
|
||||
Key: hotel, Value: 16 / Key: lima, Value: 98 / Key: bravo, Value: 56 / Key: alpha, Value: 34 /
|
||||
Key: kilo, Value: 43 / Key: delta, Value: 87 / Key: golf, Value: 34 / Key: foxtrot, Value: 12 /
|
||||
sorted:
|
||||
Key: alpha, Value: 34 / Key: bravo, Value: 56 / Key: charlie, Value: 23 / Key: delta, Value: 87 /
|
||||
Key: echo, Value: 56 / Key: foxtrot, Value: 12 / Key: golf, Value: 34 / Key: hotel, Value: 16 /
|
||||
Key: indio, Value: 87 / Key: juliet, Value: 65 / Key: kilo, Value: 43 / Key: lima, Value: 98 /
|
||||
*/
|
||||
|
Reference in New Issue
Block a user