1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-17 11:12:28 +08:00

feat: add WriteBytesToFile and WriteStringToFile

This commit is contained in:
dudaodong
2023-05-31 16:56:18 +08:00
parent 2c71b6375c
commit 75ed359084
3 changed files with 153 additions and 0 deletions

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
}