在 Go 中附加到文件 [英] Append to a file in Go

查看:16
本文介绍了在 Go 中附加到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我可以像这样从本地文件中读取:

So I can read from a local file like so:

data, error := ioutil.ReadFile(name)

我可以写入本地文件

ioutil.WriteFile(filename, content, permission)

但是我怎样才能附加到文件中呢?有内置方法吗?

But how can I append to a file? Is there a built in method?

推荐答案

这个答案在 Go1 中有效:

This answers works in Go1:

f, err := os.OpenFile(filename, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
if err != nil {
    panic(err)
}

defer f.Close()

if _, err = f.WriteString(text); err != nil {
    panic(err)
}

这篇关于在 Go 中附加到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆