-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathexample-write-file.go
34 lines (32 loc) · 1022 Bytes
/
example-write-file.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
//-------------------------------------------------------------------
// @copyright 2018 DennyZhang.com
// Licensed under MIT
// https://www.dennyzhang.com/wp-content/mit_license.txt
//
// File: example_write_file.go
// Author : Denny <https://www.dennyzhang.com/contact>
// Description : go run example_write_file.go
// Link: https://www.golang-book.com/books/intro/13
// https://cheatsheet.dennyzhang.com/cheatsheet-golang-A4
// --
// Created : <2018-04-07>
// Updated: Time-stamp: <2018-10-06 16:40:26>
//-------------------------------------------------------------------
package main
import (
"fmt"
"os"
)
func main() {
// https://stackoverflow.com/questions/7151261/append-to-a-file-in-go
file, err := os.OpenFile("/tmp/test.txt", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
// handle the error here
fmt.Print("Cannot create file", err)
return
}
defer file.Close()
fmt.Fprintf(file, "hello: %s\n", "world")
fmt.Fprint(file, "closing the file\n")
}
// File: example_write_file.go ends