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

feat: add WirteCsvFile

This commit is contained in:
dudaodong
2023-06-08 14:40:37 +08:00
parent 23a0135947
commit e2fc2f1bc9
6 changed files with 139 additions and 5 deletions

View File

@@ -454,6 +454,28 @@ func ReadCsvFile(filepath string) ([][]string, error) {
return records, nil
}
// WriteCsvFile write content to target csv file.
// Play: todo
func WriteCsvFile(filepath string, records [][]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()
writer := csv.NewWriter(f)
writer.Comma = ','
return writer.WriteAll(records)
}
// WriteStringToFile write string to target file.
// Play: https://go.dev/play/p/GhLS6d8lH_g
func WriteStringToFile(filepath string, content string, append bool) error {