登录
首页 >  Golang >  Go问答

Golang 与 PostgreSQL - 无法在 ST_DWithin 查询中传递变量

来源:stackoverflow

时间:2024-02-23 20:06:25 150浏览 收藏

大家好,今天本人给大家带来文章《Golang 与 PostgreSQL - 无法在 ST_DWithin 查询中传递变量》,文中内容主要涉及到,如果你对Golang方面的知识点感兴趣,那就请各位朋友继续看下去吧~希望能真正帮到你们,谢谢!

问题内容

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

这是我需要帮助的代码。尝试将纬度等传递到下面的查询中,但无法做到。在 node 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

正确答案


感谢@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
)

文中关于的知识介绍,希望对你的学习有所帮助!若是受益匪浅,那就动动鼠标收藏这篇《Golang 与 PostgreSQL - 无法在 ST_DWithin 查询中传递变量》文章吧,也可关注golang学习网公众号了解相关技术文章。

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>