gountries

Gountries provides: 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.

  • 所有者: pariz/gountries
  • 平台:
  • 許可證: MIT License
  • 分類:
  • 主題:
  • 喜歡:
    0
      比較:

Github星跟蹤圖

gountries

wercker status codecov.io Go Report Card

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


import (
  "github.com/pariz/gountries"
  "fmt"
)


query := gountries.New()

/////////////////
// Find sweden //
/////////////////

sweden, _ := query.FindCountryByName("sweden")
// sweden, _ := query.FindCountryByAlpha("SE")
// sweden, _ := query.FindCountryByAlpha("SWE")

fmt.Println(sweden.Name.Common) // Output: Sweden
fmt.Println(sweden.Name.Official) // Output: Konungariket Sverige

fmt.Println(sweden.Translations["DEU"].Common) // Output: Schweden
fmt.Println(sweden.Translations["DEU"].Official) // Output: Königreich Schweden


A bit more advanced


import (
  "github.com/pariz/gountries"
  "fmt"
)

query := gountries.New()

////////////////////////////////////////////
// Find the bordering countries of Sweden //
////////////////////////////////////////////

sweden, _ := query.FindCountryByAlpha("SWE") // "SE" also works..

// Get the bordering countries of sweden
for _, country := range sweden.BorderingCountries() {
	fmt.Println(country.Name.Common)
}

// Output:
// Finland
// Norway

////////////////////////////////////
// Find all subdivisons for Sweden //
////////////////////////////////////

subdivisions := sweden.SubDivisions()

for _, subdivision := range subdivisions {
	fmt.Println(subdivision.Name)
}

// Output:
// Västerbottens län
// Uppsala län
// Södermanlands län
// Gotlands län
// Dalarnas län
// ...

//////////////////////////////////////////////////////////
// Find all countries bordering Germany and Switzerland //
//////////////////////////////////////////////////////////

countryQuery := Country{
	Borders: []string{
		"DEU",
		"CHE",
	},
}

countries := query.FindCountries(countryQuery)

for _, c := range countries {
	fmt.Println(c.Name.Common)
}

// Output:
// Austria
// France

///////////////////////////////////////////////////////////////////
// Calculate distance between Sweden and Germany (in Kilometers) //
///////////////////////////////////////////////////////////////////

se, _ := query.FindCountryByAlpha("SWE")
de, _ := query.FindCountryByAlpha("DEU")

distance := gountries.MeasureDistanceHaversine(se, de)
//distance := MeasureDistancePythagoras(se, de)

fmt.Println(distance)

// Output:
// 1430.1937864547901

distance = gountries.CalculateHaversine(
	se.Coordinates.MaxLatitude, se.Coordinates.MaxLongitude,
	de.Coordinates.MinLatitude, de.Coordinates.MinLongitude)

fmt.Println(distance)

// Output:
// 2641.26145088825


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

  • Province/County querying (partially complete)
  • Measurement between coordinates
  • GeoJSON information
  • Suggestions?

主要指標

概覽
名稱與所有者pariz/gountries
主編程語言Go
編程語言Go (語言數: 1)
平台
許可證MIT License
所有者活动
創建於2016-01-13 08:04:18
推送於2024-06-04 07:45:23
最后一次提交2024-06-04 09:45:23
發布數7
最新版本名稱v0.1.6 (發布於 )
第一版名稱0.1.0 (發布於 )
用户参与
星數414
關注者數15
派生數71
提交數96
已啟用問題?
問題數28
打開的問題數15
拉請求數19
打開的拉請求數3
關閉的拉請求數9
项目设置
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?