Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

356
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda