用变量 GO111MODULE 开启或关闭模块支持,它有三个可选值:off、on、auto,默认值是 auto。
GO111MODULE=off 无模块支持,go 会从 GOPATH 和 vendor 文件夹寻找包。
GO111MODULE=on 模块支持,go 会忽略 GOPATH 和 vendor 文件夹,只根据 go.mod 下载依赖。
GO111MODULE=auto 在 $GOPATH/src 外面且根目录有 go.mod 文件时,开启模块支持。
在使用模块的时候,GOPATH 是无意义的,不过它还是会把下载的依赖储存在 $GOPATH/src/mod 中,
也会把 go install 的结果放在 $GOPATH/bin 中。
golang版本为1.14。设置方式为 go env -w GO111MODULE=on
goland中开启go module,设置 https://goproxy.cn,direct
差点忘了go断言的类型转换
package main
import (
"encoding/json"
"fmt"
"github.com/twpayne/go-geom"
"github.com/twpayne/go-geom/encoding/geojson"
"github.com/twpayne/go-geom/encoding/wkt"
"reflect"
)
func main() {
unitSquare := geom.NewPolygon(geom.XY).MustSetCoords([][]geom.Coord{
{{0, 0}, {1, 0}, {1, 1}, {0, 2}, {0, 0}},
})
fmt.Printf("unitSquare.Area() == %f", unitSquare.Area())
// geom -> wkt
unitSquareWkt, _ := wkt.Marshal(unitSquare)
fmt.Println(unitSquareWkt)
// wkt -> geom
if geomFromWkt, err := wkt.Unmarshal("POLYGON ((0 0, 1 0, 1 1, 0 2, 0 0))"); err != nil {
fmt.Println(err)
} else {
if p, ok := geomFromWkt.(*geom.Polygon); ok != true {
fmt.Println(ok)
} else {
coords := p.Coords()
for i, v := range coords {
for j, v2 := range v {
fmt.Printf("arr[%v][%v]=%v \t \n", i, j, v2)
}
fmt.Println()
}
}
fmt.Println(reflect.TypeOf(geomFromWkt).Elem())
}
// geom -> geomJson
bytes, _ := geojson.Marshal(unitSquare)
fmt.Println(string(bytes))
// geoJson -> geom
var geomFromGeojson geom.T;
a := `{"type":"Polygon","coordinates":[[[0,0],[1,0],[1,1],[0,2],[0,0]]]}`
if err := geojson.Unmarshal([]byte(a), &geomFromGeojson); err != nil {
fmt.Println(err)
} else {
if p, ok := geomFromGeojson.(*geom.Polygon); ok != true {
fmt.Println(ok)
} else {
coords := p.Coords()
for i, v := range coords {
for j, v2 := range v {
fmt.Printf("arr[%v][%v]=%v \t \n", i, j, v2)
}
fmt.Println()
}
}
fmt.Println(reflect.TypeOf(geomFromGeojson).Elem())
}
// feature -> featureJson
feature := geojson.Feature{}
feature.Geometry = geomFromGeojson
properties := make(map[string]interface{})
properties["prop0"] = "value0"
properties["prop1"] = "1.2"
feature.Properties = properties
featureJson, _ := feature.MarshalJSON()
fmt.Println(string(featureJson))
// featureJson -> feature
var m map[string]interface{}
json.Unmarshal(featureJson,&m)
marshal, _ := json.Marshal(m["geometry"])
fmt.Println(string(marshal))
// featureCollectionJson
var features [3]*geojson.Feature
features[0] = &feature
features[1] = &feature
features[2] = &feature
featureCollecation1 := geojson.FeatureCollection{Features: features[:2]}
featureCollecationJSON1, _ := featureCollecation1.MarshalJSON()
fmt.Println(string(featureCollecationJSON1))
fmt.Println(len(featureCollecation1.Features))
featureCollecation2 := geojson.FeatureCollection{}
featureCollecation2.Features = append(featureCollecation2.Features, &feature)
featureCollecation2.Features = append(featureCollecation2.Features, &feature)
featureCollecationJSON2, _ := featureCollecation2.MarshalJSON()
fmt.Println(string(featureCollecationJSON2))
fmt.Println(len(featureCollecation2.Features))
point := geom.NewPoint(geom.XY).MustSetCoords(geom.Coord{1.0, 1.0})
pointWkt, _ := wkt.Marshal(point)
fmt.Println(pointWkt)
multipoint := geom.NewMultiPoint(geom.XY).MustSetCoords([]geom.Coord{{1.0, 1.0}, {2, 2}})
multipointWkt, _ := wkt.Marshal(multipoint)
fmt.Println(multipointWkt)
line := geom.NewLineString(geom.XY).MustSetCoords([]geom.Coord{{1.0, 1.0}, {2, 2}})
lineWkt, _ := wkt.Marshal(line)
fmt.Println(lineWkt)
mutiline := geom.NewMultiLineString(geom.XY)
mutiline.Push(line)
mutiline.Push(line)
mutiline.SetSRID(4326)
mutilineWkt, _ := wkt.Marshal(mutiline)
fmt.Println(mutilineWkt)
}
有疑问加站长微信联系(非本文作者)