From 6d32b1a3431dba378d22fa8b187ccfeab259cbe2 Mon Sep 17 00:00:00 2001 From: Kevin Burke Date: Tue, 28 Feb 2017 16:28:34 -0800 Subject: [PATCH] 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 Run-TryBot: Ian Lance Taylor TryBot-Result: Gobot Gobot --- src/os/example_test.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/os/example_test.go b/src/os/example_test.go index 07f9c76959..d10bab4e1b 100644 --- a/src/os/example_test.go +++ b/src/os/example_test.go @@ -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)