A Go package for resolving city names to their administrative divisions (states/regions) using GeoNames data.
- Fast lazy-loading: Only loads country data when needed
- Alternative name support: Finds cities by their English names, local names, and alternatenames
- Pre-generated lookup maps: Serialized Go data files for fast access
- 249 countries supported
import "github.com/hunter-io/geonames"
result, err := geonames.ResolveState("DE", "Berlin")
if err != nil {
log.Fatal(err)
}
fmt.Printf("ISO Code: %s, State: %s\n", result.ISOCode, result.State)
// Output: ISO Code: DE-BE, State: BETo regenerate the data files from GeoNames:
go run scripts/download_geonames.goThis will:
- Download GeoNames zip files for all countries
- Extract and parse the data
- Generate Go files with embedded binary data using
//go:embed
resolver.go- Main API for resolving cities to statescity_alternatives.go- City name variant handlingmapping.go- GeoNames admin codes to ISO 3166-2 mappingdata/generated/- Generated lookup maps (one per country)scripts/download_geonames.go- Data generation script
This package uses data from geonames.org and was inspired by the mkrou/geonames Go parsing library.
MIT