-
Notifications
You must be signed in to change notification settings - Fork 1
/
os.go
61 lines (52 loc) · 1.22 KB
/
os.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
58
59
60
61
package writer
import (
"os"
"github.com/3JoB/unsafeConvert"
"github.com/3JoB/ulib/fsutil"
)
type oo struct {
os *os.File
}
// Higher performance sustainable file write operations.
//
// Example Files: `writer_test.go`
func NewOSWriter(path string) (*oo, error) {
n := &oo{}
var err error
n.os, err = fsutil.OpenFile(path, os.O_WRONLY|os.O_CREATE, 0666)
if err != nil {
return nil, err
}
return n, nil
}
// Write data of type `any` to the buffer (automatically checked)
func (n *oo) Add(w any) (err error) {
switch s := w.(type) {
case string:
err = n.AddString(s)
case []byte:
err = n.AddBytes(s)
default:
_, err = n.os.Write(unsafeConvert.BytePointer(w.(string)))
}
return
}
// Write data of type `[]byte` to the buffer
func (n *oo) AddBytes(w []byte) error {
_, err := n.os.Write(w)
return err
}
// Write data of type `String` to the buffer
func (n *oo) AddString(w string) error {
_, err := n.os.Write(unsafeConvert.BytePointer(w))
return err
}
// Write the data in the buffer to the file and close the IO channel.
//
// Tips: After this operation, please do not continue to operate on the previous pointer!
func (n *oo) Close() error {
if err := n.os.Close(); err != nil {
return err
}
return nil
}