Documentation
¶
Overview ¶
Package phone parses and enriches phone numbers from every country and region supported by Google's libphonenumber metadata.
Given a number and an optional country hint, Lookup returns a flat Info value with the fields commonly used by Chinese phone-number lookup pages:
号码 (number), 国家/地区 (country), 归属地 (location), 运营商 (carrier), 号段 (prefix), 区号 (area code), 邮编 (zip code), 类型 (type) and 时区 (timezone).
Global parsing, validation, formatting and number-type detection are powered by github.com/nyaruka/phonenumbers/v2 (a port of Google's libphonenumber). Mainland China lookup data (city, carrier, area code, zip code) comes from an embedded 500k-row 号段 database, so Chinese numbers resolve to a city rather than only to a country.
Basic usage:
info, err := phone.Lookup("13128500205", phone.WithCountry("CN"))
if err != nil {
log.Fatal(err)
}
fmt.Println(info.Location, info.Carrier, info.Prefix, info.AreaCode)
// 广东珠海 中国联通 131 0756
International-format numbers do not need a country hint:
info, err := phone.Lookup("+8613128500205")
Version, MetadataVersion and ChinaDataVersion report the library and embedded data versions, which services can log or expose to show which snapshots they are using.
Index ¶
Examples ¶
Constants ¶
const Version = "0.3.0"
Version is the released version of this library.
Variables ¶
var ErrCountryRequired = errors.New("phone: a country hint is required for national-format numbers")
ErrCountryRequired is returned when a number is given in national format but no country hint was supplied. Use WithCountry or the international format (leading "+" or "00") instead.
var ErrEmptyNumber = errors.New("phone: empty phone number")
ErrEmptyNumber is returned when the input is empty.
var ErrUnknownCountry = errors.New("phone: unknown country")
ErrUnknownCountry is returned when the country hint cannot be resolved.
Functions ¶
func ChinaDataVersion ¶
func ChinaDataVersion() string
ChinaDataVersion reports the version of the embedded mainland-China 号段 database, for example "2025-02".
func MetadataVersion ¶ added in v0.2.0
func MetadataVersion() string
MetadataVersion returns the upstream Google libphonenumber metadata version embedded by the parsing engine, for example "v9.0.40".
The value is compiled into the library, so it can be logged or exposed by services that need to prove which numbering-plan data they are using.
Types ¶
type Country ¶
type Country struct {
// ISO is the ISO 3166-1 alpha-2 region code, for example "CN".
ISO string `json:"iso"`
// CallingCode is the country calling code, for example 86.
CallingCode int `json:"calling_code"`
// Name is the Chinese name, for example "中国".
Name string `json:"name"`
// NameEn is the English name, for example "China".
NameEn string `json:"name_en"`
// Flag is the emoji flag, for example "🇨🇳".
Flag string `json:"flag"`
}
Country describes a country or region supported by the library.
func Countries ¶
func Countries() []Country
Countries returns all supported countries and regions, sorted by ISO code.
func CountryByCallingCode ¶
CountryByCallingCode returns the main country for a calling code, for example 86 -> China and 1 -> United States.
func CountryByISO ¶
CountryByISO returns the country with the given ISO 3166-1 alpha-2 code.
type Info ¶
type Info struct {
// Raw is the original input.
Raw string `json:"raw"`
// E164 is the number in E.164 format, for example "+8613128500205".
E164 string `json:"e164"`
// International is the number in international format, for example
// "+86 131 2850 0205".
International string `json:"international"`
// National is the number in national format, for example "131 2850 0205".
National string `json:"national"`
// RFC3966 is the number in RFC 3966 / tel: URI format.
RFC3966 string `json:"rfc3966,omitempty"`
// NationalNumber is the national significant number without the country
// calling code or national prefix, for example "13128500205".
NationalNumber string `json:"national_number"`
// CountryCode is the country calling code, for example 86.
CountryCode int `json:"country_code"`
// ISO is the ISO 3166-1 alpha-2 region code, for example "CN".
ISO string `json:"iso"`
// Country is the country/region name in Chinese, for example "中国".
Country string `json:"country"`
// CountryEn is the country/region name in English, for example "China".
CountryEn string `json:"country_en"`
// Flag is the emoji flag of the region, for example "🇨🇳".
Flag string `json:"flag,omitempty"`
// Valid reports whether the number matches the numbering plan of its
// region.
Valid bool `json:"valid"`
// Possible reports whether the number has a length that is plausible for
// its region, without checking the full numbering plan.
Possible bool `json:"possible"`
// Type is the detected number type.
Type Type `json:"type"`
// Carrier is the operator name in Chinese when available, for example
// "中国联通". For countries without Chinese carrier data it falls back to
// the English carrier name.
Carrier string `json:"carrier,omitempty"`
// CarrierEn is the operator name in English, for example "China Unicom".
CarrierEn string `json:"carrier_en,omitempty"`
// Location is the 归属地 in Chinese. For mainland China it is the
// province plus city (for example "广东珠海"); for most other countries it
// is the country name.
Location string `json:"location,omitempty"`
// LocationEn is the 归属地 in English, for example "Zhuhai, Guangdong".
LocationEn string `json:"location_en,omitempty"`
// Province is the province for mainland China mobile numbers, for example
// "广东".
Province string `json:"province,omitempty"`
// City is the city for mainland China mobile numbers, for example "珠海".
City string `json:"city,omitempty"`
// Prefix is the 号段, the leading digits of the national significant
// number, for example "131". For mainland China fixed lines it is the
// area code, for example "0756".
Prefix string `json:"prefix,omitempty"`
// AreaCode is the 区号. Mainland China area codes keep their leading zero
// ("0756"); other countries use the digits of the national destination
// code as shown by their national format ("020", "03", "202").
AreaCode string `json:"area_code,omitempty"`
// ZipCode is the postal code of the area, currently only available for
// mainland China mobile numbers.
ZipCode string `json:"zip_code,omitempty"`
// Timezones lists the IANA time zones of the number, for example
// ["Asia/Shanghai"].
Timezones []string `json:"timezones,omitempty"`
// Extension is the extension if the input contained one.
Extension string `json:"extension,omitempty"`
}
Info is the result of Lookup. Fields that a particular country does not provide are empty strings; Timezones is omitted when unknown.
Carrier information is inferred from number prefixes and may be wrong for ported numbers (number portability), which is the same caveat libphonenumber documents for its carrier mapper.
func Lookup ¶
Lookup parses raw and enriches it with country, location, carrier, prefix, area code, type and timezone information.
Numbers in international format ("+" or "00" prefix) are parsed without a country hint. Numbers in national format require WithCountry:
info, err := phone.Lookup("13128500205", phone.WithCountry("CN"))
info, err := phone.Lookup("+8613128500205")
A parsed but invalid number is returned with Info.Valid == false and a nil error; only unparseable input or a bad country hint returns an error.
Example ¶
ExampleLookup shows a lookup for a mainland China mobile number. National format needs a country hint.
package main
import (
"fmt"
phone "github.com/neko233-com/phone-lib-go"
)
func main() {
info, err := phone.Lookup("13128500205", phone.WithCountry("CN"))
if err != nil {
fmt.Println("error:", err)
return
}
fmt.Println(info.E164)
fmt.Println(info.National)
fmt.Println(info.Country, info.Location, info.Carrier)
fmt.Println(info.Prefix, info.AreaCode, info.ZipCode)
fmt.Println(info.Type)
}
Output: +8613128500205 131 2850 0205 中国 广东珠海 中国联通 131 0756 519000 mobile
Example (International) ¶
ExampleLookup_international shows that "+" and "00" numbers need no hint.
package main
import (
"fmt"
phone "github.com/neko233-com/phone-lib-go"
)
func main() {
for _, raw := range []string{"+8613128500205", "008613128500205"} {
info, err := phone.Lookup(raw)
if err != nil {
fmt.Println("error:", err)
return
}
fmt.Println(info.E164, info.ISO)
}
}
Output: +8613128500205 CN +8613128500205 CN
func MustLookup ¶
MustLookup is like Lookup but panics on error.
func Parse ¶
Parse is a shortcut for Lookup(raw, WithCountry(country)).
Example ¶
ExampleParse shows Parse as a shortcut for Lookup with a country hint.
package main
import (
"fmt"
phone "github.com/neko233-com/phone-lib-go"
)
func main() {
info, err := phone.Parse("2025550123", "US")
if err != nil {
fmt.Println("error:", err)
return
}
fmt.Println(info.E164)
fmt.Println(info.National)
fmt.Println(info.Country, info.LocationEn)
fmt.Println(info.Type, info.AreaCode, info.Timezones)
}
Output: +12025550123 (202) 555-0123 美国 Washington D.C. fixed_line_or_mobile 202 [America/New_York]
type Option ¶
type Option func(*config)
Option configures Lookup.
func WithCountry ¶
WithCountry supplies a country hint for numbers written in national format. It accepts an ISO 3166-1 alpha-2 code ("CN", "US"), a calling code ("86", "+86"), a Chinese or English country name ("中国", "United States"), or a common alias ("香港", "USA"). If the number starts with "+" or "00" the hint is not needed and is ignored.
type Type ¶
type Type int
Type is the type of a phone number, mirroring libphonenumber's PhoneNumberType values.
const ( TypeUnknown Type = iota TypeFixedLine TypeMobile TypeFixedLineOrMobile TypeTollFree TypePremiumRate TypeVoIP TypePersonalNumber TypePager TypeUAN TypeVoicemail )
Phone number types.
func (Type) MarshalJSON ¶
MarshalJSON encodes a Type as its English name.
func (*Type) UnmarshalJSON ¶
UnmarshalJSON accepts either the English name produced by MarshalJSON (for example "mobile") or the underlying integer value.
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
phone
command
Command phone is a small 114-style phone number lookup CLI built on github.com/neko233-com/phone-lib-go.
|
Command phone is a small 114-style phone number lookup CLI built on github.com/neko233-com/phone-lib-go. |
|
examples
|
|
|
web
command
Command web is a self-contained web UI demo for the phone library, styled after a 114 directory-enquiry page: a blue header, a search card and a result grid for 归属地, 运营商, 号段, 区号, 邮编, 类型, 时区 and 校验.
|
Command web is a self-contained web UI demo for the phone library, styled after a 114 directory-enquiry page: a blue header, a search card and a result grid for 归属地, 运营商, 号段, 区号, 邮编, 类型, 时区 and 校验. |
|
internal
|
|
|
cndata
Package cndata exposes the embedded mainland-China mobile number prefix database (号段库).
|
Package cndata exposes the embedded mainland-China mobile number prefix database (号段库). |