go how to write append file by using ioutil

huangapple go评论67阅读模式
英文:

go how to write append file by using ioutil

问题

你好!以下是你要翻译的内容:

我尝试使用以下代码:

appendStr := []byte("append test")
ioutil.WriteFile("./test.txt", appendStr, os.ModeAppend)

但似乎没有起作用。

英文:

I try to use the following code

appendStr := []byte("append test")
ioutil.WriteFile("./test.txt", appendStr, os.ModeAppend)

but it seems does not work

答案1

得分: 2

如JimB已经解释过的那样:

appendStr := []byte("\n 追加测试")
f, err := os.OpenFile("text.log", os.O_APPEND, 0666)
if err != nil {
fmt.Println(err)
return
}
defer f.Close()
n, err := f.Write(appendStr)
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("写入了 %d 个字节\n", n)

英文:

As JimB already explained

appendStr := []byte("\n append test")
f, err := os.OpenFile("text.log", os.O_APPEND, 0666)
if err != nil {
    fmt.Println(err)
    return
}
defer f.Close()
n, err := f.Write(appendStr)
if err != nil {
    fmt.Println(err)
    return
}
fmt.Printf("wrote %d bytes\n", n)

huangapple
  • 本文由 发表于 2022年5月2日 23:29:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/72088975.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定