Nir Shihor
Nir Shihor

Reputation: 31

Golang with postgres - unable to pass variable into a ST_DWithin query

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

Upvotes: 1

Views: 441

Answers (1)

Chandan
Chandan

Reputation: 11807

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
)

Upvotes: 3

Related Questions