-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile.go
More file actions
43 lines (34 loc) · 1.06 KB
/
file.go
File metadata and controls
43 lines (34 loc) · 1.06 KB
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
package utils
import "os"
import "io"
type fileUtils struct{}
// single variable acting as the FileUtils "subpackage" inside the legit utils package
var File fileUtils
// Checks if the file with the given path exists, returns true if yes
func (dummyReceiver *fileUtils) Exists(name string) bool {
if _, err := os.Stat(name); err != nil {
if os.IsNotExist(err) {
return false
}
}
return true
}
func (dummyReceiver *fileUtils) Delete(filePath string) error {
return os.Remove(filePath)
}
// CheckClose is used to check the return from Close in a defer statement.
// Typical usage would be to pass the pointer of the error that is returned by
// the caller function:
//
// defer checkClose(out, &err)
// io.Copy(out, resp.Body)
// return err
//
// In some scenarios, it is possible the file was closed before the defer statement
// This function insures that an error is still captured in that case
func (dummyReceiver *fileUtils) CheckClose(c io.Closer, err *error) {
cerr := c.Close()
if *err == nil {
*err = cerr
}
}