Go語言程式讀取現有檔案中的行
在 Go 程式語言中,將使用 Bufio 和 io 包函式從給定檔案讀取文字。在本文中,我們將使用三個示例從現有檔案讀取行。在第一個示例中,我們將使用 bufio 包中的 NewReader 函式,在第二個示例中,我們將使用 ioutil 包中的 ReadFile 函式,在第三個示例中,我們將分別使用 file.Read 函式。
語法
func Split(str, sep string) []string
Split() 函式用於透過提供的分隔符分割字串。此函式存在於 strings 包中,它接受要分割的字串以及分隔符作為引數。然後,該函式返回最終的字串陣列作為結果。
bufio.NewReader()
此函式屬於 Go 的 bufio 包。此函式的主要目標是以較大的塊而不是逐行讀取資料,並將其儲存在緩衝區中。io.reader 和緩衝區大小作為引數傳遞給此函式。
os.Open()
此函式是 os 包的一部分。它用於開啟檔案以進行讀取。它接收一個輸入,即要開啟的檔名。
ioutil.ReadFile()
此函式在 ioutil 包中可用,用於讀取以檔名作為函式輸入的檔案內容。
File.Read(file name)
Read 方法返回兩個值:實際讀取的字元數 (n) 和錯誤 (err)。我們檢查是否存在錯誤,如果存在,則檢查錯誤是否為 EOF(檔案結束)錯誤。如果不是 EOF 錯誤,我們將列印錯誤訊息並返回。
演算法
在程式中匯入所需的包
建立主函式
在主函式中開啟檔案並讀取其內容
如果遇到錯誤,則在控制檯上列印它
示例 1
在此示例中,我們將編寫一個 Go 語言程式,透過使用 bufio 包中提供的 NewReader() 函式從現有檔案讀取行。然後使用 reader 的 ReadString 方法從檔案中讀取行。
package main import ( "bufio" "fmt" "io" "os" ) func main() { file, err := os.Open("file.txt") if err != nil { fmt.Println("Error opening file:", err) return } defer file.Close() reader := bufio.NewReader(file) fmt.Println("Data recieved after reading from the file is:\n") for { line, err := reader.ReadString('\n') if err == io.EOF { break } if err != nil { fmt.Println("Error reading line:", err) return } fmt.Println(line) } }
輸出
Data received after reading from the file is: History of Computers It is very difficult to find the exact origin of computers. But according to some experts computer exists at the time of world war-II. Also, at that time they were used for keeping data. But, it was for only government use and not for public use.
示例 2
在此示例中,我們將編寫一個 Go 語言程式,透過使用 strings 包中提供的 Split 函式和 ioutil 包中提供的 ReadFile() 函式從現有檔案中讀取行。
package main import ( "fmt" "io/ioutil" "strings" ) func main() { contents, err := ioutil.ReadFile("file.txt") if err != nil { fmt.Println("Error reading file:", err) return } lines := strings.Split(string(contents), "\n") fmt.Println("Data received after reading lines from the file specified is:\n") for _, line := range lines { fmt.Println(line) } }
輸出
Data received after reading lines from the file specified is: History of Computers It is very difficult to find the exact origin of computers. But according to some experts computer exists at the time of world war-II. Also, at that time they were used for keeping data. But, it was for only government use and not for public use. Above all, in the beginning, the computer was a very large and heavy machine.
示例 3
在 Golang 中讀取檔案行的第三種方法是使用迴圈逐行讀取檔案。os.Open 函式用於開啟檔案,file.Read 方法用於將檔案內容讀取到位元組切片中。然後使用 bytes.IndexByte 函式在位元組切片中查詢換行符,並使用 string 函式將切片轉換為字串。
package main import ( "bytes" "fmt" "io" "os" ) func main() { file, err := os.Open("file.txt") if err != nil { fmt.Println("Error opening file:", err) return } defer file.Close() var buffer bytes.Buffer for { b := make([]byte, 1024) n, err := file.Read(b) if err == io.EOF { break } if err != nil { fmt.Println("Error reading file:", err) return } buffer.Write(b[:n]) for { index := bytes.IndexByte(buffer.Bytes(), '\n') if index == -1 { break } line := buffer.Next(index + 1) fmt.Println(string(line)) } } }
輸出
The history of programming languages spans from documentation of early mechanical computers to modern tools for software development. Early programming languages were highly specialized, relying on mathematical notation and similarly obscure Syntax. Throughout the 20th century, research in compiler theory led to the creation of high-level programming languages.
結論
我們已成功編譯並執行了一個 Go 語言程式,用於從現有檔案讀取行,並附帶示例。我們在這裡使用了三個示例來實現結果。根據用例,其中一個示例可能比其他示例更合適。無論使用哪種方法,在 Golang 中讀取檔案時正確處理錯誤都很重要。