country

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2025 License: MIT Imports: 4 Imported by: 0

README

Country Package

Go Reference Go Report Card Tests

A lightweight Go package providing ISO 3166-1 country code lookups and validations.

Features

  • Lookup countries by:
    • Name (case-insensitive)
    • Alpha-2 code (e.g., "US")
    • Alpha-3 code (e.g., "USA")
    • ISO numeric code (e.g., 840)
  • Simplified country existence checks
  • Zero dependencies (except embedded JSON data)

Installation

go get github.com/zaffka/country-go@latest

Usage

import "github.com/zaffka/country-go"

// Lookup by name
c, err := country.ByName("canada") // Case-insensitive

// Lookup by Alpha-2 code
c, err := country.ByAlpha2Code("ca")

// Check if code exists
if country.ExistsAlpha2("FR") {
    // France exists
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// List contains all countries as a slice of Ident structs.
	List []Ident

	// ListedByName provides quick lookup by country name (uppercase).
	ListedByName map[string]*Ident

	// ListedByAlpha2 provides quick lookup by 2-letter country code (uppercase).
	ListedByAlpha2 map[string]*Ident

	// ListedByAlpha3 provides quick lookup by 3-letter country code (uppercase).
	ListedByAlpha3 map[string]*Ident

	// ListedByIsoNum provides quick lookup by ISO numeric country code.
	ListedByIsoNum map[uint16]*Ident

	// ErrNotFound is returned when a country lookup fails.
	ErrNotFound = errors.New("country not found")
)

Functions

func Exists

func Exists(name string) bool

Exists checks if a country with the given name exists (case-insensitive).

func ExistsAlpha2

func ExistsAlpha2(code string) bool

ExistsAlpha2 checks if a country with the given Alpha2 code exists (case-insensitive).

func ExistsAlpha3

func ExistsAlpha3(code string) bool

ExistsAlpha3 checks if a country with the given Alpha3 code exists (case-insensitive).

func ExistsISONum

func ExistsISONum(isoNum uint16) bool

ExistsISONum checks if a country with the given ISO numeric code exists.

func ListLen

func ListLen() int

ListLen returns the total number of countries in the dataset.

Types

type Ident

type Ident struct {
	// Name - is an official country name in uppercase (e.g., "CANADA").
	Name string `json:"name"`

	// Alpha2 - 2-letter country code (e.g., "CA").
	Alpha2 string `json:"alpha2"`

	// Alpha3 - 3-letter country code (e.g., "CAN").
	Alpha3 string `json:"alpha3"`

	// ISONum - ISO numeric country code (e.g., 124).
	ISONum uint16 `json:"iso_num"`
}

Ident represents a country with its standard identifiers. All fields use official ISO 3166-1 naming and codes.

func ByAlpha2Code

func ByAlpha2Code(code string) (*Ident, error)

ByAlpha2Code finds a country by its 2-letter ISO code (case-insensitive). Returns ErrNotFound if no matching country exists.

func ByAlpha3Code

func ByAlpha3Code(code string) (*Ident, error)

ByAlpha3Code finds a country by its 3-letter ISO code (case-insensitive). Returns ErrNotFound if no matching country exists.

func ByISONum

func ByISONum(isoNum uint16) (*Ident, error)

ByISONum finds a country by its ISO numeric code. Returns ErrNotFound if no matching country exists.

func ByName

func ByName(name string) (*Ident, error)

ByName finds a country by its name (case-insensitive). Returns ErrNotFound if no matching country exists.

Jump to

Keyboard shortcuts

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