forked from eswaribala/rps_cis_go_2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOpenClose.go
37 lines (31 loc) · 869 Bytes
/
OpenClose.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
35
36
37
package main
import (
"log"
"os"
)
func main() {
// Simple read only open. We will cover actually reading
// and writing to files in examples further down the page
file, err := os.Open("test2.txt")
if err != nil {
log.Fatal(err)
}
file.Close()
// OpenFile with more options. Last param is the permission mode
// Second param is the attributes when opening
file, err = os.OpenFile("test2.txt", os.O_APPEND, 0666)
if err != nil {
log.Fatal(err)
}
file.Close()
// Use these attributes individually or combined
// with an OR for second arg of OpenFile()
// e.g. os.O_CREATE|os.O_APPEND
// or os.O_CREATE|os.O_TRUNC|os.O_WRONLY
// os.O_RDONLY // Read only
// os.O_WRONLY // Write only
// os.O_RDWR // Read and write
// os.O_APPEND // Append to end of file
// os.O_CREATE // Create is none exist
// os.O_TRUNC // Truncate file when opening
}