附加到Go中的文件 [英] Append to a file in Go
本文介绍了附加到Go中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
data,error:= ioutil.ReadFile(name)
我可以写入本地文件
ioutil.WriteFile(文件名,内容,权限)
我怎样才能追加到一个文件?有没有内置的方法?
解决方案
这个答案在Go1中有效:
如果错误!=零(
panic(err),
$ f $, )
}
推迟f.Close()
if _,err = f.WriteString(text); err!= nil {
panic(err)
}
So I can read from a local file like so:
data, error := ioutil.ReadFile(name)
And I can write to a local file
ioutil.WriteFile(filename, content, permission)
But how can I append to a file? Is there a built in method?
解决方案
This answers works in Go1:
f, err := os.OpenFile(filename, os.O_APPEND|os.O_WRONLY, 0600)
if err != nil {
panic(err)
}
defer f.Close()
if _, err = f.WriteString(text); err != nil {
panic(err)
}
这篇关于附加到Go中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文