country_mapper

package module
v0.0.0-...-5553e86 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 21, 2019 License: MIT Imports: 5 Imported by: 0

README

Country Info Mapper in Go

Build Status

Installation

go get github.com/pirsquare/country-mapper

Examples

package main

import (
	"fmt"
	"github.com/pirsquare/country-mapper"
)

var countryClient *country_mapper.CountryInfoClient

func init() {
	client, err := country_mapper.Load()
	if err != nil {
		panic(err)
	}

	countryClient = client
}

func main() {
	data := countryClient.MapByName("South Korea")
	fmt.Println(data.Name)           // Will Print: South Korea
	fmt.Println(data.Alpha2)         // Will Print: KR
	fmt.Println(data.Alpha3)         // Will Print: KOR
	fmt.Println(data.Currency[0])    // Will Print: KRW
	fmt.Println(data.CallingCode[0]) // Will Print: 82
	fmt.Println(data.Region)         // Will Print: Asia
	fmt.Println(data.Subregion)      // Will Print: Eastern Asia

	// you can try different variations of name, but be careful though,
	// only commonly used names is supported
	data = countryClient.MapByName("south korea")
	fmt.Println(data.Name) // Will Print: South Korea

	data = countryClient.MapByName("대한민국")
	fmt.Println(data.Name) // Will Print: South Korea

	data = countryClient.MapByName("southkorea")
	fmt.Println(data == nil) // Will Print: true

	// You can also use different variations of mappings:

	// Map by Country Code Alpha-2
	data = countryClient.MapByAlpha2("SG")
	fmt.Println(data.Name) // Will Print: Singapore

	// Map by Country Code Alpha-3
	data = countryClient.MapByAlpha3("SGP")
	fmt.Println(data.Name) // Will Print: Singapore

	// Get all countries that uses Aussie dollar as currency
	dataList := countryClient.MapByCurrency("AUD")
	for _, row := range dataList {
		fmt.Println(row.Name)
		// Will Print:
		// Australia
		// Christmas Island
		// Cocos (Keeling) Islands
		// Heard Island and McDonald Islands
		// Kiribati
		// Nauru
		// Norfolk Island
		// Tuvalu
	}

	// Get all countries that uses "61" as calling code
	dataList = countryClient.MapByCallingCode("61")
	for _, row := range dataList {
		fmt.Println(row.Name)
		// Will Print:
		// Australia
		// Christmas Island
		// Cocos (Keeling) Islands
	}

	// Get all countries from Oceania Region
	dataList = countryClient.MapByRegion("Oceania")
	for _, row := range dataList {
		fmt.Println(row.Name)
		// Will Print:
		// American Samoa
		// Australia
		// Christmas Island
		// Cocos (Keeling) Islands
		// Cook Islands
		// Fiji
		// French Polynesia
		// Guam
		// Kiribati
		// Marshall Islands
		// Micronesia
		// Nauru
		// New Caledonia
		// New Zealand
		// Niue
		// Norfolk Island
		// Northern Mariana Islands
		// Palau
		// Papua New Guinea
		// Pitcairn Islands
		// Samoa
		// Solomon Islands
		// Tokelau
		// Tonga
		// Tuvalu
		// Vanuatu
		// Wallis and Futuna
	}

	// Get all countries from South Eastern Asia
	dataList = countryClient.MapBySubregion("South-Eastern Asia")
	for _, row := range dataList {
		fmt.Println(row.Name)
		// Will Print:
		// Brunei
		// Cambodia
		// Indonesia
		// Laos
		// Malaysia
		// Myanmar
		// Philippines
		// Singapore
		// Thailand
		// Timor-Leste
		// Vietnam
	}
}

FAQ

How can I use my own csv file for country's data

You can pass in an optional url to country_mapper.Load("http"//example.com/file.csv"). This is useful if you prefer to host the data file yourself or if you have modified some of the fields for your specific use case. Do keep in mind that the schema and order of columns should still be kept the same.

Credits

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CountryInfo

type CountryInfo struct {
	Name           string
	AlternateNames []string
	Alpha2         string
	Alpha3         string
	Capital        string
	Currency       []string
	CallingCode    []string
	Region         string
	Subregion      string
}

func (*CountryInfo) AlternateNamesLower

func (c *CountryInfo) AlternateNamesLower() []string

func (*CountryInfo) CallingCodeLower

func (c *CountryInfo) CallingCodeLower() []string

func (*CountryInfo) CurrencyLower

func (c *CountryInfo) CurrencyLower() []string

type CountryInfoClient

type CountryInfoClient struct {
	Data []*CountryInfo
}

func Load

func Load(specifiedURL string, remote bool) (*CountryInfoClient, error)

Pass in an optional url if you would like to use your own downloadable csv file for country's data. This is useful if you prefer to host the data file yourself or if you have modified some of the fields for your specific use case. You can now pass in location to a local repo

func (*CountryInfoClient) MapByAlpha2

func (c *CountryInfoClient) MapByAlpha2(alpha2 string) *CountryInfo

func (*CountryInfoClient) MapByAlpha3

func (c *CountryInfoClient) MapByAlpha3(alpha3 string) *CountryInfo

func (*CountryInfoClient) MapByCallingCode

func (c *CountryInfoClient) MapByCallingCode(callingCode string) []*CountryInfo

func (*CountryInfoClient) MapByCurrency

func (c *CountryInfoClient) MapByCurrency(currency string) []*CountryInfo

func (*CountryInfoClient) MapByName

func (c *CountryInfoClient) MapByName(name string) *CountryInfo

func (*CountryInfoClient) MapByRegion

func (c *CountryInfoClient) MapByRegion(region string) []*CountryInfo

func (*CountryInfoClient) MapBySubregion

func (c *CountryInfoClient) MapBySubregion(subregion string) []*CountryInfo

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL