go how to write append file by using ioutil

Issue

I try to use the following code

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

but it seems does not work

Solution

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)

Answered By – Manjeet Thakur

Answer Checked By – Willingham (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.