0

I am using the following code to wrap execution of aquery on a postgres database in a API I am building just to learn Go.

func QueryDB(qstring string) (*sql.Rows) {
    psqlInfo := fmt.Sprintf("host=%s port=%d user=%s dbname=%s sslmode=disable", host, port, user, dbname)
    db, err := sql.Open("postgres", psqlInfo)
    if err != nil {
      panic(err)
    }
    defer db.Close()

    //Ping method opens the connection
    err = db.Ping()
    if err != nil {
       panic(err)
    }

    //rows, err := db.Query("SELECT name FROM users WHERE age = $1", age)
    log.Printf("executing SQL %s\n",qstring)
    rows, err := db.Query(qstring)
    if err != nil {
       log.Fatal(err)
    }
    return rows
}

my question is as per the documentation the db.Query() method accepts a query strgin and ...interface{} arguments otherwise. tried adding a second argument params ...interface{} to my function arguments and passing params as the second argument to the DB.Query method, and this doesn't work. it only seems to work if I specifically pull the arguments out of the interface slice. Any suggestions?

3
  • 3
    It's a variadic function: golang.org/ref/spec#Passing_arguments_to_..._parameters Commented Jul 23, 2018 at 20:51
  • 1
    BTW, sql.Open is meant to open and maintain a connection pool to the database. You will want to Open it once, and reuse the handle it returns. The library itself will maintain the base connection and spin up/down any necessary additional connections. Commented Jul 23, 2018 at 22:12
  • Since you say you are learning Go, I'll add this as well. "Don't Panic!" is one of the Go mottos. In Go, you handle errors on the spot, or return them to the caller to handle. If your code ends up as a library some day and it panics, it terminates someone's program. Generally it is OK to Panic in your own Main function if something like a one-off sql.Open fails because that is something you would want to terminate your own program. It's something your application simply cannot function without. Commented Jul 23, 2018 at 22:50

2 Answers 2

2

I figured it out by lookingin the source code of the SQL module and seeing how it was implemented in the db.Query() method. you add the elipsis to the end of the slice argument to pass it through. Like params...

Sign up to request clarification or add additional context in comments.

1 Comment

Peek into the standard libraries often and you will learn much.
1

This may help you

Variadic functions can be called in the usual way with individual arguments. If you already have multiple args in a slice, apply them to a variadic function using func(slice...)

If you want send data from an slice, just add elipsis:

rows, err := db.Query(qstring, data...)
if err != nil {
   log.Fatal(err)
}

If you has the values separated in different variables:

rows, err := db.Query(qstring, val1, val2, val3, valN)
if err != nil {
   log.Fatal(err)
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.