How to read the whole file content into a string in Go?
如何在Go中将整个文件内容读取为字符串 ?
You can use the ReadFile() func from the io/ioutil package in Go to read the whole file content:
您可以使用Go的io/ioutil 包中的ReadFile()函数来读取整个文件内容:
func ReadFile(filename string) ([]byte,error)
For example, read file filepath content and check the error:
例如,读取文件文件filepath内容并检查错误:
if bytes, err := ioutil.ReadFile(filepath); err != nil {
log.Fatal("Failed to read file: " + filepath)
}
Answered by Eric Z Ma.
埃里克·马(Eric Z Ma)回答。
翻译自: https://www.systutorials.com/how-to-read-the-whole-file-content-into-a-string-in-go/
|