如何在Go中将整个文件内容读取为字符串?

论坛 期权论坛 编程之家     
选择匿名的用户   2021-6-2 20:25   2253   0

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/

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP