golang port of Google's libphonenumber
This is a port of https://github.com/ttacon/libphonenumber
Forked to add go module-version.
This library is fully stable and is used in production.
When Google's libphonenumber is updated, run make update
, commit metagen.go
, and push a new sem-ver tag, and then run make docs
, to distribute the version.
Super simple to use.
num, err := libphonenumber.Parse("6502530000", "US")
// num is a *libphonenumber.PhoneNumber
formattedNum := libphonenumber.Format(num, libphonenumber.NATIONAL)
// Parse the number.
num, err := libphonenumber.Parse("1234567890", "US")
if err != nil {
// Handle error appropriately.
}
// Get the cleaned number and the length of the area code.
natSigNumber := libphonenumber.GetNationalSignificantNumber(num)
geoCodeLength := libphonenumber.GetLengthOfGeographicalAreaCode(num)
// Extract the area code.
areaCode := ""
if geoCodeLength > 0 {
areaCode = natSigNumber[0:geoCodeLength]
}
fmt.Println(areaCode)