-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfile.go
More file actions
37 lines (33 loc) · 712 Bytes
/
file.go
File metadata and controls
37 lines (33 loc) · 712 Bytes
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 (
"os"
)
func checkFile(filename string) {
if !FileExists(filename) {
fail("File not found: " + filename)
}
}
func saveFile(dst string, content string) {
out, err := os.Create(dst)
checkFail(err, "File could not be created: "+dst)
defer func() {
cerr := out.Close()
if err == nil {
err = cerr
}
}()
_, err = out.WriteString(content)
checkFail(err, "File could not be written: "+dst)
err = out.Sync()
checkFail(err, "File could not be persisted: "+dst)
return
}
// Exists reports whether the named file or directory exists.
func FileExists(name string) bool {
if _, err := os.Stat(name); err != nil {
if os.IsNotExist(err) {
return false
}
}
return true
}