Answer the question
In order to leave comments, you need to log in
How to use an array in a database query from Golang?
Hello comrades! Help please deal with the problem. Golang
application
has an array. In my case it looks like this .
I am trying to use this array in an Oracle database query. I am using goracle library for this purpose. My code looks like this:[27625 27626 27627]
args := make([]interface{}, len(ids))
for i, id := range ids {
args[i] = id
}
stmt := `SELECT ORGANIZATION_ID, ORGANIZATION_NAME FROM ORG WHERE ORGANIZATION_ID IN (:value` + strings.Repeat(",:value", len(args)-1) + `)`
fmt.Println(stmt)
// return: SELECT ORGANIZATION_ID, ORGANIZATION_NAME FROM ORG WHERE ORGANIZATION_ID IN (:value,:value,:value)
fmt.Println(args)
// return: [27625 27626 27627]
rows, err := database.OracleDB.Query(stmt, args); if err != nil {
utils.ResponseWithError(responseWriter, http.StatusInternalServerError, err.Error())
return
}
1. arg: unknown type []interface {}
Answer the question
In order to leave comments, you need to log in
looks like you need to pass a pointer to a string instead of a stringargs[i] = &id
The function expects multiple arguments of type , and you pass one type to it . There is syntactic sugar for passing arguments to a variadic functionQuery(query string, args ...interface{})
interface{}
[]interface{}
database.OracleDB.Query(stmt, args...) //ellipses ...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question