简体   繁体   English

如何使用go pg CRUD Postgres Point数据类型

[英]How to CRUD Postgres Point data type using go pg

I am using Point datatype for storing coordinates in Postgres DB. 我使用Point数据类型在Postgres DB中存储坐标。 How do I map Point datatype to Go lang datatypes? 如何将Point数据类型映射到Go lang数据类型? I am not finding any documentation for the same. 我没有找到相同的任何文件。

go-pg does not have native support to Point type (as of PostGIS). go-pg没有Point类型的原生支持(从PostGIS开始)。 What I've done to overpass that (may not be the best solution but I did manage to make it work) was to put on my model individual Latitude and Longitude fields, and on the query itself use ColumnExpr to get individual values using ST_X (for longitude) and ST_Y (for latitude, don't forget that). 我所做的就是跨越那个(可能不是最好的解决方案,但我确实设法使其工作)是在我的模型上设置单独的纬度和经度字段,并且在查询本身上使用ColumnExpr来使用ST_X获取单个值(对于经度)和ST_Y (对于纬度,不要忘记)。

Model: 模型:

type MyModel struct {
    ID              int64
    Name            string
    LocationLat     float64
    LocationLon     float64
}

Query: 查询:

err := db.Model(&myModel).
        Column("id", "name").
        ColumnExpr("ST_X(location) AS location_lon").
        ColumnExpr("ST_Y(location) AS location_lat").
        Where("id = ?", id).
        Select()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM