Golang 与 postgres - 无法将变量传递到 ST_DWithin 查询中

发布于 2025-01-11 07:17:07 字数 746 浏览 0 评论 0原文

对于 Golang 来说非常陌生,无法解决这个问题。任何帮助将不胜感激。

这是我需要帮助的代码。尝试将纬度等传递到下面的查询中,但无法做到。在节点 js 中寻找 ${latitude} 的等效项...

   latitude := r.URL.Query().Get("lat")
   longitude := r.URL.Query().Get("lon")
   radius := r.URL.Query().Get("rad")


   row := db.QueryRow(`SELECT "name", "website", "coordinates", 
   "description", "rating"
    FROM geospots
    WHERE ST_DWithin("coordinates", ST_MakePoint(latitude = $1, 
   longitude = $2)::geography, radius = $3)`, latitude, longitude)


   Error: 
   andlers/SpotsInArea.go:15:5: latitude declared but not used
   handlers/SpotsInArea.go:16:5: longitude declared but not used
   handlers/SpotsInArea.go:17:5: radius declared but not used

Very new to Golang and unable to resolve this. Any help would be highly appreciated.

This is the code I need help with. Trying to pass the latitude etc into the query below, but unable to do it. Looking for the equivalent on ${latitude} in node js...

   latitude := r.URL.Query().Get("lat")
   longitude := r.URL.Query().Get("lon")
   radius := r.URL.Query().Get("rad")


   row := db.QueryRow(`SELECT "name", "website", "coordinates", 
   "description", "rating"
    FROM geospots
    WHERE ST_DWithin("coordinates", ST_MakePoint(latitude = $1, 
   longitude = $2)::geography, radius = $3)`, latitude, longitude)


   Error: 
   andlers/SpotsInArea.go:15:5: latitude declared but not used
   handlers/SpotsInArea.go:16:5: longitude declared but not used
   handlers/SpotsInArea.go:17:5: radius declared but not used

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

以歌曲疗慰 2025-01-18 07:17:07

感谢@mkopirva

正如所指出的,您不需要 latitude = $1 它应该只是 $1,您已经在查询中定义了 $3 但缺少第三个值

并确保变量在您访问它们的范围内

row := db.QueryRow(`
SELECT
    "name",
    "website",
    "coordinates",
    "description",
    "rating"
FROM
    geospots
WHERE
    ST_DWithin(
        "coordinates",
        ST_MakePoint($1, $2)::geography,
        $3
    )`,
    longitude, latitude, radius
)

Thanks to @mkopirva

As pointed you don't need latitude = $1 it should be just $1, You have defined $3 in query but missing the 3rd value

And make sure the variable are within the scope where you are accessing them

row := db.QueryRow(`
SELECT
    "name",
    "website",
    "coordinates",
    "description",
    "rating"
FROM
    geospots
WHERE
    ST_DWithin(
        "coordinates",
        ST_MakePoint($1, $2)::geography,
        $3
    )`,
    longitude, latitude, radius
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文