countryphone

countryphone resolves international phone number prefixes into country and regional metadata.

Part of nurago, a collection of independent Go packages for backend services.

import "github.com/tecnickcom/nurago/pkg/countryphone"

Package countryphone resolves international phone number prefixes into country and regional metadata.

The package models country calling code behavior defined by the International Telecommunication Union (ITU), including the ITU-T E.123 and E.164 numbering recommendations.

How it works

countryphone indexes prefix data in a trie and performs longest-prefix lookup, giving deterministic matches for variable-length numbering plans. The default dataset is embedded, and callers can provide custom data through New for provider-specific or updated plans.

Typical usage

Create a resolver with New, then call NumberInfo for a prefix or full number. When no prefix matches, NumberInfo returns an error.

When To Use

  • You classify phone numbers by origin from the prefix alone.
  • Longest-prefix matching is required, because prefixes overlap.

Example

// load default data
data := countryphone.New(nil)

info, err := data.NumberInfo("1357123456")
if err != nil {
	log.Fatal(err)
}

b, err := json.MarshalIndent(info, "", "  ")
if err != nil {
	log.Fatal(err)
}

fmt.Println(string(b))

// Output:
// {
//   "type": 1,
//   "geo": [
//     {
//       "alpha2": "US",
//       "area": "California",
//       "type": 1
//     }
//   ]
// }

Full source is in example_countryphone_test.go. More runnable examples are on pkg.go.dev.

Dependencies

This package reaches no external module: it uses only the Go standard library.