Golang Join array interface

前端 未结 1 1900
别那么骄傲
别那么骄傲 2020-12-20 19:14

I try to create bulk insert. I use gorm github.com/jinzhu/gorm

import (
    "fmt"
    dB "github.com/edwinlab/api/repositories"
)

func Up         


        
相关标签:
1条回答
  • 2020-12-20 20:02

    If you want to pass elements of a slice to a function with variadic parameter, you have to use ... to tell the compiler you want to pass all elements individually and not pass the slice value as a single argument, so simply do:

    tx.Exec(sqlStr, vals...)
    

    This is detailed in the spec: Passing arguments to ... parameters.

    Tx.Exec() has the signature of:

    func (tx *Tx) Exec(query string, args ...interface{}) (Result, error)
    

    So you have to pass vals.... Also don't forget to check returned error, e.g.:

    res, err := tx.Exec(sqlStr, vals...)
    if err != nil {
        // handle error
    }
    
    0 讨论(0)
提交回复
热议问题