我有很多小文件,我不想逐行阅读。
Go 中是否有将整个文件读入字符串变量的函数?
最佳答案
使用 ioutil.ReadFile
:
func ReadFile(filename string) ([]byte, error)
ReadFile reads the file named by filename and returns the contents. A successful call returns err == nil, not err == EOF. Because ReadFile reads the whole file, it does not treat an EOF from Read as an error to be reported.
你会得到一个 []byte
而不是 string
。如果真的需要,可以转换:
s := string(buf)
关于string - 如何将整个文件读入字符串变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13514184/