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

refactor: update signature of WriteMapsToCsv function

g
This commit is contained in:
dudaodong
2024-01-24 11:54:42 +08:00
parent e0c9ccbce3
commit be62aaac9b
9 changed files with 46 additions and 46 deletions

View File

@@ -45,6 +45,7 @@ import (
- [Sha](#Sha)
- [ReadCsvFile](#ReadCsvFile)
- [WriteCsvFile](#WriteCsvFile)
- [WriteMapsToCsv](#WriteMapsToCsv)
- [WriteStringToFile](#WriteStringToFile)
- [WriteBytesToFile](#WriteBytesToFile)
- [ReadFile](#ReadFile)
@@ -754,7 +755,8 @@ func main() {
// records: 写入文件的map切片。map值必须为基本类型。会以map键的字母顺序写入。
// appendToExistingFile: 是否为追加写模式。
// delimiter: CSV文件分割符。
func WriteMapsToCsv(filepath string, records []map[string]string, append_to_existing_file bool, delimiter ...rune) error
// headers: CSV文件表头顺序需要与map key保持一致),不指定时按字母排序。
func WriteMapsToCsv(filepath string, records []map[string]any, appendToExistingFile bool, delimiter rune, headers ...[]string) error
```
<b>示例:</b>
@@ -779,7 +781,8 @@ func main() {
{"Name": "Jim", "Age": "21", "Gender": "male"},
}
err := fileutil.WriteMapsToCsv(csvFilePath, records, false, ';')
headers := []string{"Name", "Age", "Gender"}
err := WriteMapsToCsv(csvFilePath, records, false, ';', headers)
if err != nil {
log.Fatal(err)
@@ -790,7 +793,7 @@ func main() {
fmt.Println(content)
// Output:
// [[Age Gender Name] [22 female Lili] [21 male Jim]]
// [[Name Age Gender] [Lili 22 female] [Jim 21 male]]
}
```

View File

@@ -45,6 +45,7 @@ import (
- [Sha](#Sha)
- [ReadCsvFile](#ReadCsvFile)
- [WriteCsvFile](#WriteCsvFile)
- [WriteCsvFile](#WriteCsvFile)
- [WriteMapsToCsv](#WriteMapsToCsv)
- [WriteStringToFile](#WriteStringToFile)
- [WriteBytesToFile](#WriteBytesToFile)
@@ -755,7 +756,8 @@ func main() {
// records: slice of maps to be written. the value of map should be basic type. The maps will be sorted by key in alphabeta order, then be written into csv file.
// appendToExistingFile: If true, data will be appended to the file if it exists.
// delimiter: Delimiter to use in the CSV file.
func WriteMapsToCsv(filepath string, records []map[string]string, append_to_existing_file bool, delimiter ...rune) error
// headers: order of the csv column headers, needs to be consistent with the key of the map.
func WriteMapsToCsv(filepath string, records []map[string]any, appendToExistingFile bool, delimiter rune, headers ...[]string) error
```
<b>Example:</b>
@@ -780,7 +782,8 @@ func main() {
{"Name": "Jim", "Age": "21", "Gender": "male"},
}
err := fileutil.WriteMapsToCsv(csvFilePath, records, false, ';')
headers := []string{"Name", "Age", "Gender"}
err := fileutil.WriteMapsToCsv(csvFilePath, records, false, ';', headers)
if err != nil {
log.Fatal(err)
@@ -791,7 +794,7 @@ func main() {
fmt.Println(content)
// Output:
// [[Age Gender Name] [22 female Lili] [21 male Jim]]
// [[Name Age Gender] [Lili 22 female] [Jim 21 male]]
}
```