file-io - 在 Go 中附加到文件

标签 file-io go

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

data, error := ioutil.ReadFile(name)

我可以写入本地文件

ioutil.WriteFile(filename, content, permission)

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

最佳答案

此答案适用于 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)
}

关于file-io - 在 Go 中附加到文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7151261/

相关文章:

vim - 使用 'go fmt' 工具正常运行的 Vim 进行注释

jquery - Ajax新手学习(golang jquery)

c - 错误 : undeclared here (not in a function)

Java,如何制作一个jar来编辑另一个jar

python - 从大文件(~2.5GB)读取并存储到 python 列表时出现 MemoryError

go - 我应该如何使用 protoc-gen-go-grpc?

c - 结构数组作为参数传递给 read() 问题

java - PrintWriter 未写入文件(Java)

compilation - 在 Go 中构建和引用我自己的本地包

go - 在 Go 中异步执行命令和 ZIP 文件夹