Preface
I studied the file library and finally found a way to use Go to append content to the end of the file
The main 2 functions:
func (f *File) Seek(offset int64, whence int) (ret int64, err error) func (f *File) WriteAt(b []byte, off int64) (n int, err error)
Seek()
Check the offset at the end of the file
WriteAt()
Then start writing from the offset
Here are examples:
// fileName: file name (with full path)// content: the written contentfunc appendToFile(fileName string, content string) error { // Open the file in a write-only mode f, err := (fileName, os.O_WRONLY, 0644) if err != nil { (" file create failed. err: " + ()) } else { // Find the offset at the end of the file n, _ := (0, os.SEEK_END) // Start writing content from the end offset _, err = ([]byte(content), n) } defer () return err}
Summarize
The editor thinks that the document blogs of golang in China are still a little lacking. I hope that everyone will share their experiences in coding, so that golang can become more and more useful! The above is the entire content of this article. I hope it will be helpful to everyone's study or work. If you have any questions, you can leave a message to communicate.