-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile.go
More file actions
32 lines (27 loc) · 698 Bytes
/
file.go
File metadata and controls
32 lines (27 loc) · 698 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
package goutil
import (
"fmt"
"os"
)
// OpenFile is a function for open file with path & filename
func OpenFile(path, filename string) (*os.File, error) {
os.Mkdir(path, 0777)
return os.OpenFile(
fmt.Sprintf("%s/%s", path, filename),
os.O_CREATE|os.O_APPEND|os.O_RDWR,
0666)
}
// OpenFileNewest is a function for open file with path & filename and close the oldest os.File
func OpenFileNewest(oldest *os.File, path, filename string) (*os.File, error) {
file, err := os.OpenFile(
fmt.Sprintf("%s/%s", path, filename),
os.O_CREATE|os.O_APPEND|os.O_RDWR,
0666)
if err != nil {
return nil, err
}
if err = oldest.Close(); err != nil {
return nil, err
}
return file, nil
}