Search code examples
gocassandravariadic-functionsgocql

Variadic arguments in cassandra query with gocql


I want to make a generic function for performing cassandra queries using the gocql client, something like :

queryExec("INSERT INTO USERS VALUES(?,?,?,?)", userId, emailId, mobileNo, gender)

func queryExec(query string, args ...interface{}) err{
err := session.query(query, args).Exec()
return err

}

but when I pass it multiple argument values, it gives me the following error :

gocql : expected 4 values send got 1

Solution

  • It should be

    err := session.query(query, args...).Exec()
    

    Without the ellipsis, query receives a slice containing all the args.