1
0
mirror of https://github.com/golang/go synced 2024-11-18 16:04:44 -07:00

os: add OpenFile example for appending data

Fixes #19329.

Change-Id: I6d8bb112a56d751a6d3ea9bd6021803cb9f59234
Reviewed-on: https://go-review.googlesource.com/37619
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
Kevin Burke 2017-02-28 16:28:34 -08:00 committed by Ian Lance Taylor
parent 9bd1cc3fa1
commit 6d32b1a343

View File

@ -21,6 +21,20 @@ func ExampleOpenFile() {
}
}
func ExampleOpenFile_append() {
// If the file doesn't exist, create it, or append to the file
f, err := os.OpenFile("access.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Fatal(err)
}
if _, err := f.Write([]byte("appended some data\n")); err != nil {
log.Fatal(err)
}
if err := f.Close(); err != nil {
log.Fatal(err)
}
}
func ExampleChmod() {
if err := os.Chmod("some-filename", 0644); err != nil {
log.Fatal(err)