penguin/golifehk

@golifehk bot in Telegram

query/geo_test.go

raw ยท 671 bytes

package query

import (
	"fmt"
	"testing"
)

type City struct {
	Name string
	NoWords
	GeoLocation
}

func TestGeo(t *testing.T) {

	B := GeoLocations{
		City{Name: "Osaka", GeoLocation: GeoLocation{34.6937, 135.5023}},
		City{Name: "Nagoya", GeoLocation: GeoLocation{35.1815, 136.9066}},
		City{Name: "Sapporo", GeoLocation: GeoLocation{43.0618, 141.3545}},
		City{Name: "Yokohama", GeoLocation: GeoLocation{35.4437, 139.6380}},
	}

	// Reference point A (Tokyo)
	A := GeoLocation{35.6895, 139.6917}

	// Sort by distance
	B.SortByNearest(A)

	// Print result
	for _, loc := range B {
		fmt.Printf("%s: %.2f km\n", loc.(City).Name, loc.Dist(A.Lat(), A.Lon())/1000)
	}
}