gountries
Inspired by the countries gem for ruby.
Countries (ISO-3166-1), Country Subdivisions(ISO-3166-2), Currencies (ISO 4217), Geo Coordinates(ISO-6709) as well as translations, country borders and other stuff exposed as struct data.
All data is derived from the pariz/countries repo.
This is currently a work in progress, so things may change. More stuff will be added
Installation
go get github.com/pariz/gountries
Examples
Basic
package main
import (
"fmt"
"github.com/pariz/gountries"
)
func main() {
query := gountries.New()
sweden, _ := query.FindCountryByName("sweden")
fmt.Println(sweden.Name.Common)
fmt.Println(sweden.Name.Official)
fmt.Println(sweden.Translations["DEU"].Common)
fmt.Println(sweden.Translations["DEU"].Official)
}
A bit more advanced
package main
import (
"fmt"
"github.com/pariz/gountries"
)
func main() {
query := gountries.New()
sweden, _ := query.FindCountryByAlpha("SWE")
for _, country := range sweden.BorderingCountries() {
fmt.Println(country.Name.Common)
}
subdivisions := sweden.SubDivisions()
for _, subdivision := range subdivisions {
fmt.Println(subdivision.Name)
}
countryQuery := gountries.Country{
Borders: []string{
"DEU",
"CHE",
},
}
countries := query.FindCountries(countryQuery)
for _, c := range countries {
fmt.Println(c.Name.Common)
}
se, _ := query.FindCountryByAlpha("SWE")
de, _ := query.FindCountryByAlpha("DEU")
distance := gountries.MeasureDistanceHaversine(se, de)
fmt.Println(distance)
distance = gountries.CalculateHaversine(
se.Coordinates.MaxLatitude, se.Coordinates.MaxLongitude,
de.Coordinates.MinLatitude, de.Coordinates.MinLongitude)
fmt.Println(distance)
}
Using packed data
The data in the data/yaml
subdirectory is embedded using go-bindata. Once you include this library in your project, you won't need to access the data directory. To add or update the data, make changes to the YAML files then run:
go-bindata -pkg gountries data/yaml/*
Testing
Has a pretty solid test coverage but is constantly improving.
Todo