Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6784369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:59:05+00:00 2026-05-26T16:59:05+00:00

A package I am using, gosqlite, has a method with a variadic parameter where

  • 0

A package I am using, gosqlite, has a method with a variadic parameter where its type is the empty interface.

func (s *Stmt) Exec(args ...interface{}) os.Error

I can call this fine if explicitly pass individual parameters:

statement := blah()
error := statement.Exec("hello", 3.0, true) // works fine

However, as the variadic parameter corresponds to placeholders within the in operator of my SQL statement’s select, the number of these placeholders is not known at compile time but dynamically changes at run time depending upon what the user is doing. E.g. I end up with SQL akin to the following if the user enters four values:

SELECT * FROM sky WHERE name IN (?,?,?,?)

So naturally I would like to call the Exec method with a slice of strings:

var values []string = getValuesFromUser()
statement := createStatementWithSufficientNumberOfPlaceholders(len(values))
_ := statement.Exec(values...) // compiler doesn't like this

This does not compile. I can get around this problem by creating an empty interface slice and copying the references over:

values2 := make([]interface{}, len(values))
for index, value := range values { values2[index] = value }
_ := statement.Exec(values2...) // compiler happy but I'm not

And this works fine but it feels a bit clunky. I was wondering if there was some trick to be able to pass values directly to this function or, failing that, a neater way of converting the string slice to an empty interface one?

Many thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T16:59:05+00:00Added an answer on May 26, 2026 at 4:59 pm

    There is no way to pass a []string directly to a ...interface{} parameter. Doing this requires a linear time copy (with n + 1 allocations!). If the language hid this from you, it would be a significant hidden cost. Normally, passing a slice to a variadic argument just passes the slice into the function.

    As for other ways of doing this, you could make it cleaner by writing a function that takes a []string and returns the corresponding []interface{}. Of course, you’ll have to write it again for each []T -> []interface{} conversion you want to do, but its a rather short function, and all that changes is the signature. You could use reflection, which comes with an inherent runtime cost, to make the function “generic”, such as in:

    valuesVal := reflect.ValueOf(values)
    ...
    for i := range values2 { values2[i] = valuesVal.Index(i).Interface() } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If one installs a python package using setuptools, then executes a method in that
Possible Duplicate: Cannot install R-forge package using install.packages Has anyone gotten the latest version
I am using package qvalue which has the following pre-processing step if(min(p)<0 || max(p)>1)
I have created an installation package using Wix which installs a Windows service on
I'm calling a SSIS package using LoadPackage(...). Is it possible to make this call
I am building a Setup Package using VS2008. This is a regular setup package
I'm monkey patching a package using a technique given at the beginning of How
I've been documenting a software package using Sphinx and reStructuredText . Within my documents,
I have installed the vim-latex package using Ubuntu Synaptic package manager and updated my
Hi I am using package-maker of Mac OS to create installer package for Mac

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.