title: How to create mysql database record dynamically
body: <p>How would it be possible to create a database record (in this case mysql) without literally specifying each variable to be passed in to the model, on or at the model (see following example). With having to create dozens of models, I am trying to avoid having to specify these vars manually with each and every model. I can dynamically collect this list on the controller and am hoping there is some way to just pass one object from the controller to the model (in this failed experiment: res []string).</p>
  <pre><code>// controller
  _, err := policy.CreateItem(c.DB, res)
  
  // model
  func CreateItem(db Connection, res []string) (sql.Result, error) {
  // res = ParentID, Title, Description, Sort, Date, StatusID, UserID
  tfcim := format.TitlesForCreateInputModel(ItemMin{}) // parent_id, title, description, sort, date, status_id, user_id
  phfcm := format.PlaceHoldersForCreateModel(ItemMin{}) // ?,?,?,?,?,?,?
  result, err := db.Exec(fmt.Sprintf(`
          INSERT INTO %v
          (`+tfcim+`)
          VALUES
          (`+phfcm+`)
          `, table),
      strings.Join(res, &quot;, &quot;)) // &lt;----------------- How can magic happen here?
  return result, err
  }
  </code></pre>
  <p>And logically, the output is: flight.go:138: sql: expected 7 arguments, got 1</p>
  <p>To note, ItemMin{} is the table struct returning the table keys using 'reflect' in a package named format.</p>
  <p>Any functional method, even an entirely different approach (except using an ORM/GORM) would be acceptable. Thanks in advance!</p>
  <p><strong>UPDATE: Complete working solution:</strong></p>
  <pre><code>// controller
  s := make([]interface{}, len(res))
  for i, v := range res {
      s[i] = v
  }
  _, err := policy.CreateItem(c.DB, s)
  
  // model
  func CreateItem(db Connection, res []interface{}) (sql.Result, error) {
      tfcim := format.TitlesForCreateInputModel(ItemMin{})  // parent_id, title, description, sort, date, status_id, user_id
      phfcm := format.PlaceHoldersForCreateModel(ItemMin{}) // ?,?,?,?,?,?,?
      query := fmt.Sprintf(&quot;INSERT INTO %v (%v) VALUES (%v)&quot;, table, tfcim, phfcm)
      result, err := db.Exec(query, res...)
      return result, err
   }
  </code></pre>