I'm trying to extract the names of .txt files in a folder.
But I encounter an issue where the write function from encoding/csv stop writing in the csv file I initialize.
The code is as follow.
import (
"encoding/csv"
"io/ioutil"
"log"
"os"
"strings"
)
func Extract() error {
//read folder
AllFiles, err := ioutil.ReadDir("./folder")
if err != nil {
return err
}
//create csv file
SupportCsv, err := os.Create("csvfile.csv")
if err != nil {
return err
}
WriterSupport := csv.NewWriter(SupportCsv)
for _, file := range AllFiles {
res := []string{"test", file.Name()}
log.Println(res)
//write in csv file
err = WriterSupport.Write(res)
if err != nil {
return err
}
}
return nil
}
I have ~300 .txt file in my folder.
The log.Println prints everything so the issue must be with the Write function.
In return the csv file as less file name and the last name is not written entirely.
Moreover, when i change the number of letter on "test" string in res, the csv file as in return more or less file name in it.
Write returns no error.
defer SupportCsv.Close().