1 回答
TA贡献1877条经验 获得超1个赞
你应该移动queries.Close()到循环的末尾,像这样:
var GetQuestions = func(responseWriter http.ResponseWriter, request *http.Request) {
rows, _ := database.DBSQL.Query("SELECT * FROM questions;")
defer rows.Close()
var questions []Question
for rows.Next() {
var question Question
if err := rows.Scan(&question.ID, &question.Text); err != nil {
log.Println(err)
continue
}
queries, _ := database.DBSQL.Query(`select options.id as option_id, options.text as option_text from questions_options inner join questions on questions_options.question_id = $1 inner join options on questions_options.option_id = options.id`, question.ID)
for queries.Next() {
var option Option
if err := queries.Scan(&option.ID, &option.Text); err != nil {
log.Println(err)
}
question.Options = append(question.Options, option)
}
queries.Close()
questions = append(questions, question)
}
utils.Response(responseWriter, http.StatusOK, questions)
}
- 1 回答
- 0 关注
- 105 浏览
添加回答
举报