mirror of
https://github.com/gopl-zh/gopl-zh.github.com.git
synced 2026-03-10 11:54:30 +00:00
修复代码前的包路径
This commit is contained in:
@@ -4,8 +4,8 @@
|
||||
|
||||
第一個版本的dup輸出標準輸入流中的出現多次的行,在行內容前是出現次數的計數。這個程序將引入if表達式,map內置數據結構和bufio的package。
|
||||
|
||||
<u><i>gopl.io/ch1/dup1</i></u>
|
||||
```go
|
||||
gopl.io/ch1/dup1
|
||||
// Dup1 prints the text of each line that appears more than
|
||||
// once in the standard input, preceded by its count.
|
||||
package main
|
||||
@@ -78,8 +78,8 @@ dup1中的程序還出現了\t和\n的格式化字符串。這些特殊的轉義
|
||||
|
||||
很多程序像上面的例子一樣從標準輸入中讀取數據,但輸入源有時還可能是一些文件。下面的dup程序從標準輸入得到一些文件名,然後用os.Open函數來打開每一個文件獲取內容。
|
||||
|
||||
<u><i>gopl.io/ch1/dup2</i></u>
|
||||
```go
|
||||
gopl.io/ch1/dup2
|
||||
// Dup2 prints the count and text of lines that appear more than once
|
||||
// in the input. It reads from stdin or from a list of named files.
|
||||
package main
|
||||
@@ -136,8 +136,8 @@ os.Open返迴的第二個值是一個Go語言內置的error類型。如果這個
|
||||
|
||||
我們簡化了dup3這個程序。首先,它隻讀取命名的文件,而不去讀標準輸入,因爲ReadFile函數需要一個文件名參數。其次,我們將行計數邏輯移迴到了main函數,因爲現在這個邏輯隻有一個地方需要用到。
|
||||
|
||||
<u><i>gopl.io/ch1/dup3</i></u>
|
||||
```go
|
||||
gopl.io/ch1/dup3
|
||||
package main
|
||||
|
||||
import (
|
||||
@@ -172,4 +172,3 @@ ReadFile函數返迴byte類型的slice,這個slice必須被轉換爲string,
|
||||
在更底層一些的地方,bufio.Scanner,ioutil.ReadFile和ioutil.WriteFile使用的都是*os.File的Read和Write方法,不過一般程序員併不需要去直接了解到其底層實現細節,在bufio和io/ioutil包中提供的方法已經足夠好用。
|
||||
|
||||
**練習 1.4:** 脩改dup2,使其可以分别打印重複的行出現在哪些文件。
|
||||
|
||||
|
||||
Reference in New Issue
Block a user