-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_handling.go
57 lines (49 loc) · 863 Bytes
/
file_handling.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
)
func exists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
func WriteToFile(File string, content string) error {
exists, err := exists(File)
if err != nil {
return err
}
var file *os.File
if !exists {
file, err = os.Create(File)
if err != nil {
log.Fatal(err)
}
} else {
file, err = os.OpenFile(File, os.O_RDWR|os.O_APPEND, 0660)
if err != nil {
log.Fatal(err)
}
}
len, err := file.WriteString(content + "\n")
if err != nil {
return err
}
fmt.Printf("length : %d", len)
return nil
}
func ReadFromFile(File string) error {
data, err := ioutil.ReadFile(File)
if err != nil {
return err
}
fmt.Printf("\nList of movies:\n %s", data)
return nil
}