GoLang Mongo GeoJSON

Adr*_*let 1 struct go mongodb geojson

在 Golang 中,使用 MongoDB,我尝试存储 GeoJSON 对象,同时保留 2dsphere 索引。

我无法声明一个可以处理“点”和“多边形”的通用结构,因为“点”有一个[]float64坐标字段,而“多边形”有一个[][]float64坐标字段。

您知道如何声明这样的结构吗?

小智 5

您可以尝试在结构中使用 as和interface的字段。我为您的场景创建了一个简单的程序,如下所示:polygonpoint

package main

import (
    "fmt"
)

type figure struct {
    name        string
    coordinates interface{}
}

func main() {

    Point := figure{"Point", [2]float64{2.0, 7.88}}
    Polygon := figure{"Polygon", [2][2]float64{{2.0, 7.88}, {3.0, 7.88}}}
    fmt.Println(Point)
    fmt.Println(Polygon)

}
Run Code Online (Sandbox Code Playgroud)

输出:

{Point [2 7.88]}
{Polygon [[2 7.88] [3 7.88]]}
Run Code Online (Sandbox Code Playgroud)