Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

358
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda