mirror of
https://github.com/gopl-zh/gopl-zh.github.com.git
synced 2025-08-08 08:12:02 +00:00
ch4: fix code path
This commit is contained in:
@@ -155,9 +155,8 @@ equal(map[string]int{"A": 0}, map[string]int{"B": 42})
|
||||
|
||||
Go語言中併沒有提供一個set類型,但是map中的key也是不相同的,可以用map實現類似set的功能。爲了説明這一點,下面的dedup程序讀取多行輸入,但是隻打印第一次出現的行。(它是1.3節中出現的dup程序的變體。)dedup程序通過map來表示所有的輸入行所對應的set集合,以確保已經在集合存在的行不會被重複打印。
|
||||
|
||||
<u><i>gopl.io/ch4/dedup</i></u>
|
||||
```Go
|
||||
gopl.io/ch4/dedup
|
||||
|
||||
func main() {
|
||||
seen := make(map[string]bool) // a set of strings
|
||||
input := bufio.NewScanner(os.Stdin)
|
||||
@@ -195,9 +194,8 @@ func Count(list []string) int { return m[k(list)] }
|
||||
|
||||
這是map的另一個例子,下面的程序用於統計輸入中每個Unicode碼點出現的次數。雖然Unicode全部碼點的數量鉅大,但是出現在特定文檔中的字符種類併沒有多少,使用map可以用比較自然的方式來跟蹤那些出現過字符的次數。
|
||||
|
||||
<u><i>gopl.io/ch4/charcount</i></u>
|
||||
```Go
|
||||
gopl.io/ch4/charcount
|
||||
|
||||
// Charcount computes counts of Unicode characters.
|
||||
package main
|
||||
|
||||
@@ -268,9 +266,8 @@ len count
|
||||
|
||||
Map的value類型也可以是一個聚合類型,比如是一個map或slice。在下面的代碼中,圖graph的key類型是一個字符串,value類型map[string]bool代表一個字符串集合。從概念上將,graph將一個字符串類型的key映射到一組相關的字符串集合,它們指向新的graph的key。
|
||||
|
||||
<u><i>gopl.io/ch4/graph</i></u>
|
||||
```Go
|
||||
gopl.io/ch4/graph
|
||||
|
||||
var graph = make(map[string]map[string]bool)
|
||||
|
||||
func addEdge(from, to string) {
|
||||
|
Reference in New Issue
Block a user