csvgofile-handling

Golang function not reading the file names correctly


So, I've got a repository which has several .csv files, they contain the table schema for a database. I have written a Golang code that gets the list of the file names from the repo, then opens these files, reads the content and creates a MySQL CREATE query.

The issue that I'm facing is that for certain .csv files, the Golang code ends up reading the title wrong, which is causing problems in the later stages. Like for instance, there are files with the names -- config_hr.csv, config_oe.csv, contribution_analysis.csv which is being read as onfig_hr.csv, onfig_oe.csv, ontribution_analysi.csv. This issue seems to be addressed if I were to capitalize the names, but that has many other problems in the later stages of our project.

Is this some kind of an encoding issue? I have checked the code on Windows, Mac and Linux, with Golang version being the latest v1.21, any help or insight would be highly appreciated!

Snippet of the Golang code reading the names of the CSV files

entries, err := FileEntry.Readdir(0)
    if err != nil {
        log.Fatal(err)
    }

    // Now, open all the files one by one, and extract the content of the files.
    // Then modify the resultant string to be of MySQL compatibility.
    for _, e := range entries {
        // Mimicking the SQL Query of Table Creation query.
        Query_String := ("CREATE TABLE IF NOT EXISTS " + strings.ToLower(strings.Trim(strings.Replace(e.Name(), " ", "_", -1), ".csv")) + " (\n")
        fmt.Println("Opening -- " + file_folder + "/" + e.Name())
        file, err := os.Open(file_folder + "/" + e.Name())
        if err != nil {
            log.Fatal(err)
        }
        defer file.Close()
        // Reading the CSV file from path.
        reader := csv.NewReader(file)
        records, err := reader.ReadAll()
        if err != nil {
            log.Fatal(err)
        }
      

Solution

  • Replace string.Trim funtion with below function.

    // getFileNameWithoutExtension takes a file path as input and returns
    // the file name without its extension. It utilizes the filepath package
    // to extract the base name and then removes the extension.
    func getFileNameWithoutExtension(filePath string) string {
        // Get the base name of the file path (including extension)
        baseName := filepath.Base(filePath)
    
        // Calculate the length to remove the extension from the base name
        // and obtain the file name without extension
        fileNameWithoutExtension := baseName[:len(baseName)-len(filepath.Ext(baseName))]
    
        // Return the file name without extension
        return fileNameWithoutExtension
    }
    
    

    Sample code:

     Query_String := ("CREATE TABLE IF NOT EXISTS " + strings.ToLower(getFileNameWithoutExtension(strings.Replace(e.Name(), " ", "_", -1))) + " (\n")