Append text to a file
source link: https://yourbasic.org/golang/append-to-file/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Append text to a file
This code appends a line of text to the file text.log
.
It creates the file if it doesn’t already exist.
f, err := os.OpenFile("text.log",
os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Println(err)
}
defer f.Close()
if _, err := f.WriteString("text to append\n"); err != nil {
log.Println(err)
}
If you’re appending text to a file for logging purposes, see Write log to file.
Related
Follow on Twitter
Most Read
This work is licensed under a CC BY 3.0 license.
Recommend
-
37
-
28
Latest version of file 'ext/misc/appendvfs.c': Fileext/misc/appendvfs.c — part of check-in[bfbeffab] at2018-01-17 13:15:23 on branchtrunk — Fix harmless compiler warnings, mostly unused parameters for UDF...
-
53
-
40
back One issue with most backup solutions is that an attacker controlling the local system can also wipe its old backups. To prevent this the backup must pe...
-
6
How to append text or lines to a file in python? – thispointer.comIn this article, we will discuss how to append text or new lines to an existing file using python. To append some text to a file in the end, we first need to open the f...
-
9
Append a file into a zip file in Python I was trying to find a way to append a file into a zip file in python,...
-
0
[JavaScript] Append Text at the Beginning of Each Line January 11, 2018...
-
5
[sed] Append Text at the Beginning of Each Line March 15, 2016
-
4
[Golang] Append Line/String to File January 23, 2017 I summarize the answer
-
4
Run
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK