Go語言程式以只讀模式開啟檔案
在 Go 語言中,只讀模式指的是一種操作模式,在這種模式下,檔案、目錄或裝置只能被檢視或讀取,而不能被修改或刪除。此模式用於防止意外更改資料。在計算機系統中,只讀模式可以在檔案或資料夾級別設定,也可以為整個磁碟或分割槽設定。在本文中,我們將使用三個示例來演示如何以只讀模式開啟檔案。在第一個示例中,我們將使用 os 包中的 open 函式;在第二個示例中,我們將使用 ioutil 包中的 ReadFile 函式;在第三個示例中,我們將分別使用 bufio 包中的 NewReader 函式。
語法
file.read()
Read() 函式用於讀取檔案內容。該函式接受要讀取其內容的檔案作為引數,並返回檔案內容以及一個錯誤變數。
func make ([] type, size, capacity)
Go 語言中的 **make** 函式用於建立陣列/對映,它接受要建立的變數型別、其大小和容量作為引數。
ioutil.ReadFile(file)
ReadFile() 函式位於 ioutil 包中,用於讀取給定的檔案。該函式接受要讀取的檔案作為函式的引數。
os.Open()
此函式是 os 包的一部分。它用於以只讀方式開啟檔案。它接受一個輸入,即要開啟的檔名。
bufio.NewReader()
此函式屬於 Go 的 bufio 包。此函式的主要目標是以較大的塊而不是逐行讀取資料並存儲在緩衝區中。io.reader 和緩衝區大小作為引數傳遞給此函式。
示例 1
在此示例中,我們將使用 os 包中的 open() 函式來編寫一個 Go 語言程式,以只讀模式開啟檔案。我們將使用 Read() 函式來實現結果。
演算法
首先,我們需要匯入 fmt 和 os 包。
然後,啟動 main() 函式。在 main() 函式內部,定義一個名為 file 的變數,並將 os.Open 函式呼叫後獲得的結果儲存到其中,並將檔名作為引數傳遞給它。
檢查函式獲得的錯誤是否為 nil。如果不是 nil,則在螢幕上列印錯誤。
呼叫“defer file.Close()”語句,以便在函式成功執行時關閉檔案。
使用“file.Read”方法從檔案讀取。
透過將檔案內容轉換為字串,在螢幕上列印檔案內容。
package main import ( "fmt" "os" ) func main() { // Open the file in read-only mode file, err := os.Open("newfile.txt") if err != nil { panic(err) } defer file.Close() // Read the contents of the file b := make([]byte, 1024) _, err = file.Read(b) if err != nil { panic(err) } // Print the contents of the file fmt.Println("The given file is opened in read-only mode and its contents are:\n", string(b)) }
輸出
The given file is opened in read-only mode and its contents are: Hello, World!
示例 2
在此示例中,我們將編寫一個 Go 語言程式,透過使用 ioutil 包中的 readFile() 函式以只讀模式開啟檔案。此函式讀取檔案的全部內容並將其作為位元組切片返回。
演算法
首先,我們需要匯入“ioutil”和“fmt”包。
然後,啟動 main() 函式。在 main() 函式內部,定義一個名為 data 的型別為 []byte 的變數和一個名為 err 的型別為 error 的變數。
呼叫 ioutil.ReadFile 函式,並將檔名作為引數傳遞給它。
將函式的結果儲存到名為 data 的變數中。
如果函式返回的錯誤變數不為 nil,則使用 fmt.Println() 函式在螢幕上列印錯誤。
當函式成功執行時,檔案會自動關閉。
package main import ( "fmt" "io/ioutil" ) func main() { // Read the entire contents of the file data, err := ioutil.ReadFile("newfile.txt") if err != nil { panic(err) } // Print the contents of the file fmt.Println("The given file is opened in read-only mode and its contents are:\n", string(data)) }
輸出
The given file is opened in read-only mode and its contents are: Hello, World!
示例 3
在此示例中,我們將編寫一個 Go 語言程式,透過使用 bufio 包中的 newreader() 函式以只讀模式開啟檔案。此函式建立一個實現 reader 介面的新讀取器。
演算法
首先,我們需要匯入“bufio”和“os”包。
然後,啟動 main() 函式。在 main() 函式內部,呼叫 os 包中的 Open() 函式,並將檔名作為引數傳遞給它。
將函式的結果儲存到 file 變數中。
檢查錯誤變數是否不為 nil。如果不是 nil,則使用 fmt.Println() 函式在螢幕上列印錯誤。
使用 bufio.NewReader() 函式建立一個新的讀取器,並將檔名作為引數傳遞給該函式。
如果函式接收到的錯誤變數不為 nil,則在螢幕上列印錯誤;否則,列印給定檔案以讀寫模式開啟。
package main import ( "bufio" "fmt" "os" ) func main() { // Open the file in read-only mode file, err := os.Open("newfile.txt") if err != nil { panic(err) } defer file.Close() // Create a new reader reader := bufio.NewReader(file) // Read the contents of the file b, _, err := reader.ReadLine() if err != nil { panic(err) } // Print the contents of the file fmt.Println("The given file is opened in read-only mode and its contents are:\n", string(b)) }
輸出
The given file is opened in read-only mode and its contents are: Hello, World!
結論
我們已經成功編譯並執行了一個 Go 語言程式,以只讀模式開啟檔案以及示例。在第一個示例中,我們使用了 File.Read() 函式,而在第二個和第三個示例中,我們分別使用了 ReadFile() 和 NewReader() 函式。