title: How to import data from csv file into postegrsql using go?
body: <p>I have a docker container with *.csv files in it. I want to import data from that files into my postgresql database. The tables creates from schema.sql file.
  I need to import from 3 different csv files data into 3 different tables in my db.
  I tried to make it with <code>db.Exec('COPY &lt;table_name&gt; FROM &lt;path&gt; DELIMITER ',' CSV HEADER;</code> but it didn't work (error: cannot open file, or file doesn't exist)
  So i make the following function and it works fine, but i think it is not a good solution.</p>
  <pre><code>func AddSomeDataIntoTable(tableName, path string, fieldsQty int) error {
      file, err := os.Open(path)
      if err != nil {
          return err
      }
      defer file.Close()
  
      reader := csv.NewReader(file)
  
      if _, err := reader.Read(); err == io.EOF {
          return fmt.Errorf(&quot;error: empty file&quot;)
      }
  
      //loop of reading
      for i := 0; ; i++ {
          record, err := reader.Read()
          if err != nil {
              if err != io.EOF {
                  return fmt.Errorf(&quot;Error reading file: %w&quot;, err)
              }
              if i == 0 {
                  return fmt.Errorf(&quot;Malformed csv file: there's only headers and no values&quot;)
              } else {
                  log.Print(&quot;end of file&quot;)
                  //end of the file
                  return nil
              }
          }
  
          //malformed csv handling
          if len(record) != fieldsQty {
              return fmt.Errorf(&quot;Malformed csv file: wrong number of fields&quot;)
          }
          for _, v := range record {
              if v == &quot;&quot; {
                  return fmt.Errorf(&quot;malformed csv file: empty fields&quot;)
              }
          }
          _, err = uuid.Parse(record[0])
          if err != nil {
              return fmt.Errorf(&quot;malformed id, should be a uuid: %w&quot;, err)
          }
  
          switch tableName {
          case &quot;&lt;table_name1&gt;&quot;:
              _, err = db.Pdb.Exec(`INSERT INTO &lt;table_name1&gt; (field1, field2, field3, field4, field5) VALUES ($1, $2, $3, $4, $5)`,
                  record[0], record[1], record[2], record[3], record[4])
              if err != nil {
                  return fmt.Errorf(&quot;internal db problems: %w&quot;, err)
              }
          case &quot;&lt;table_name2&quot;:
              _, err = db.Pdb.Exec(`INSERT INTO table_name2 (field1, field2) VALUES ($1, $2)`,
                  record[0], record[1])
              if err != nil {
                  return fmt.Errorf(&quot;internal db problems: %w&quot;, err)
              }
          case &quot;&lt;table_name3&gt;&quot;:
              _, err = db.Pdb.Exec(`INSERT INTO table_name3 (field1, field2) VALUES ($1, $2)`,
                  record[0], record[1])
              if err != nil {
                  return fmt.Errorf(&quot;internal db problems: %w&quot;, err)
              }
          default:
              return fmt.Errorf(&quot;cannot find such table&quot;)
          }
      }
  
      return nil
  }
  </code></pre>
  <p>May be someone can advise me any other better solution?</p>