Golang 和 GIN 的全文搜索结果为空
时间:2024-02-11
浏览:381
作者:佚名
从现在开始,我们要努力学习啦!今天我给大家带来《Golang 和 GIN 的全文搜索结果为空》,感兴趣的朋友请继续看下去吧!下文中的内容我们主要会涉及到等等知识点,如果在阅读本文过程中有遇到不清楚的地方,欢迎留言呀!我们一起讨论,一起学习!
问题内容
我正在尝试使用 golang 和 gin 作为我的路由器,并使用 sqlc 进行 sql 代码生成。无论我将查询绑定为 uri 还是查询,我都会得到一个空列表。请帮忙。
885375576526下面是查询函数:
const searchproduct = `-- name: searchproduct :many
select id, name, owner, price, description, imgs_url, imgs_name, created_at, tsv
from products
where textsearchable_index_col @@ to_tsquery($2)
order by created_at desc
limit $1
`
type searchproductparams struct {
limit int32 `json:"limit"`
searchquery string `json:"search_query"`
}
func (q *queries) searchproduct(ctx context.context, arg searchproductparams) ([]product, error) {
rows, err := q.db.querycontext(ctx, searchproduct, arg.limit, arg.searchquery)
if err != nil {
return nil, err
}
defer rows.close()
items := []product{}
for rows.next() {
var i product
if err := rows.scan(
&i.id,
&i.name,
&i.owner,
&i.price,
&i.description,
pq.array(&i.imgsurl),
pq.array(&i.imgsname),
&i.createdat,
&i.tsv,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.close(); err != nil {
return nil, err
}
if err := rows.err(); err != nil {
return nil, err
}
return items, nil
}
针对我的数据库运行 sql 原始查询,我得到了带有我发送的查询的产品。 下面是sql查询:
-- name: SearchProduct :many SELECT * FROM products WHERE textsearchable_index_col @@ to_tsquery(sqlc.arg(search_query)) ORDER BY created_at DESC LIMIT $1;
我尝试将请求作为 uri 和 postman 中的表单参数传递,但这两种方法仍然返回 200 状态和空列表。请问这个函数有什么问题吗?我究竟做错了什么?这是我学习 go 的第三个月了。
正确答案
Q 因为它以小写字母开头,所以是未导出的,这意味着声明它的包之外的任何代码都无法访问它。包括反射,它由您向其传递 req 的绑定方法使用。换句话说,将 Q 更改为 Q。
这是有关此事的官方语言规范:https://go.dev/ref/spec#Exported_identifiers
文中关于的知识介绍,希望对你的学习有所帮助!若是受益匪浅,那就动动鼠标收藏这篇《Golang 和 GIN 的全文搜索结果为空》文章吧,也可关注golang学习网公众号了解相关技术文章。
本文内容来源于互联网,如有侵权请联系删除。
近期热门
11.18
11.15
站内热门
最新文章
11.29
11.29
11.29
11.29
11.29
热门专题
