feat: add WriteBytesToFile and WriteStringToFile

This commit is contained in:
dudaodong committed 2023-05-31 16:56:18 +08:00
1 parent 2c71b6375c
commit 75ed359084
3 files changed
+153

No files matched your search

+32
View File
@@ -453,3 +453,35 @@ func ReadCsvFile(filepath string) ([][]string, error) {
return records, nil
}
// WriteStringToFile write string to target file.
// Play: todo
func WriteStringToFile(filepath string, content string, append bool) error {
flag := os.O_RDWR | os.O_CREATE
if append {
flag = flag | os.O_APPEND
}
f, err := os.OpenFile(filepath, flag, 0644)
if err != nil {
return err
}
defer f.Close()
_, err = f.WriteString(content)
return err
}
// WriteBytesToFile write bytes to target file.
// Play: todo
func WriteBytesToFile(filepath string, content []byte) error {
f, err := os.OpenFile(filepath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
return err
}
defer f.Close()
_, err = f.Write(content)
return err
}