daily pastebin goal
16%
SHARE
TWEET

Untitled

a guest Oct 11th, 2018 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. func BulkInsert(unsavedRows []*ExampleRowStruct) error {
  2.     valueStrings := make([]string, 0, len(unsavedRows))
  3.     valueArgs := make([]interface{}, 0, len(unsavedRows) * 3)
  4.     i := 0
  5.     for _, post := range unsavedRows {
  6.         valueStrings = append(valueStrings, fmt.Sprintf("($%d, $%d, $%d)", i*3+1, i*3+2, i*3+3))
  7.         valueArgs = append(valueArgs, post.Column1)
  8.         valueArgs = append(valueArgs, post.Column2)
  9.         valueArgs = append(valueArgs, post.Column3)
  10.         i++
  11.     }
  12.     stmt := fmt.Sprintf("INSERT INTO my_sample_table (column1, column2, column3) VALUES %s", strings.Join(valueStrings, ","))
  13.     _, err := db.Exec(stmt, valueArgs...)
  14.     return err
  15. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top