Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

355
Views
Golang: cannot use sql.NamedArg in prepared statement

I have a query that is using NamedArgs from the sql.DB and I'm getting an error when building

cannot use args (type []sql.NamedArg) as type []interface {} in argument to stmt.Exec

The example in the SQL library shows it being used:

Example usage:
    db.ExecContext(ctx, `
     delete from Invoice
     where
     TimeCreated < @end
     and TimeCreated >= @start;`,
     sql.Named("start", startTime),
     sql.Named("end", endTime),
    )

The only difference is that I'm currently using a prepared statement stmt and calling the Exec method on that. I've created a slice of NamedArg with my values and it's using the ... expander.

res, err := stmt.Exec(args...)

What exactly is wrong when the example shows the sql.Named() method call directly in the code? Why wouldn't an expanded slice work?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

That's just how passing arguments to a variadic function works in Go. You either pass the individual values which can be of any type, or you pass in a slice whose element type matches exactly that of the variadic parameter and follow it with ....

I.e. you can either do:

res, err := stmt.Exec(
    sql.Named("start", startTime),
    sql.Named("end", endTime),
)

or you can do:

args := []interface{}{
    sql.Named("start", startTime),
    sql.Named("end", endTime),
}
res, err := stmt.Exec(args...)
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!