e164

package module
v0.0.0-...-a5e3d8e Latest Latest
Warning

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

Go to latest
Published: Jul 13, 2025 License: MIT Imports: 2 Imported by: 0

README

go-e164

Package e164 implements tools for working with E.164 formatted phone-numbers, for the Go programming language.

Documention

Online documentation, which includes examples, can be found at: http://godoc.org/github.com/reiver/go-e164

GoDoc

Example

Here is an example of parsing a phone-number if E.164 format:

countryCode, nationalDestinationCode, subscriberNumber, err := e164.ParseTolerantly("+16045551234")

Import

To import package e164 use import code like the following:

import "github.com/reiver/go-e164"

Installation

To install package e164 do the following:

GOPROXY=direct go get github.com/reiver/go-e164

Author

Package e164 was written by Charles Iliya Krempeaux

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ISOCountryCode

func ISOCountryCode(countryCode string, nationalDestinationCode string) string

ISOCountryCode is given an E.164 country-code (ex: "1", "82", "98"), and a national-destination-code (ex: "604") and returns the corresponding ISO 3166-1 alpha-2 country-code (ex: "CA", "KR", "IR").

For example:

isoCountryCode := e164.ISOCountryCode("1", "604")
// isoCountryCode == "CA"
Example (Ca)
package main

import (
	"fmt"

	"github.com/reiver/go-e164"
)

func main() {

	e164CountryCode := "1"
	e164NationalDestinationCode := "604" // Also called an area-code

	isoCountryCode := e164.ISOCountryCode(e164CountryCode, e164NationalDestinationCode)

	fmt.Printf("ISO 3166-1 alpha-2 country-code: %s\n", isoCountryCode)

}
Output:
ISO 3166-1 alpha-2 country-code: CA
Example (Kr)
package main

import (
	"fmt"

	"github.com/reiver/go-e164"
)

func main() {

	e164CountryCode := "82"
	e164NationalDestinationCode := "2" // Also called an area-code

	isoCountryCode := e164.ISOCountryCode(e164CountryCode, e164NationalDestinationCode)

	fmt.Printf("ISO 3166-1 alpha-2 country-code: %s\n", isoCountryCode)

}
Output:
ISO 3166-1 alpha-2 country-code: KR
Example (Us)
package main

import (
	"fmt"

	"github.com/reiver/go-e164"
)

func main() {

	e164CountryCode := "1"
	e164NationalDestinationCode := "206" // Also called an area-code

	isoCountryCode := e164.ISOCountryCode(e164CountryCode, e164NationalDestinationCode)

	fmt.Printf("ISO 3166-1 alpha-2 country-code: %s\n", isoCountryCode)

}
Output:
ISO 3166-1 alpha-2 country-code: US

func ParseTolerantly

func ParseTolerantly(value string) (countryCode string, nationalDestinationCode string, subscriberNumber string, err error)

ParseTolerantly parses `value` for a E.164 phone-number tolerantly, such that it allows spaces between the country-code, the national-destination-code and the subscriber-number.

The basic format is:

[+][country code][area code][local phone number]

For example:

var phoneNumber string = "+16045551234"

countryCode, nationalDestinationCode, subscriberNumber, err := e164.ParseTolerantly(phoneNumber)
// countryCode == "1"
// nationalDestinationCode == "604"
// subscriberNumber == "5551234"
Example (Ca)
package main

import (
	"fmt"

	"github.com/reiver/go-e164"
)

func main() {

	phoneNumber := "+16045551234"

	countryCode, nationalDestinationCode, subscriberNumber, err := e164.ParseTolerantly(phoneNumber)
	if nil != err {
		fmt.Printf("ERROR: %s\n", err)
		return
	}

	fmt.Printf("E.164 country-code: %s\n", countryCode)
	fmt.Printf("E.164 national-destination-code: %s\n", nationalDestinationCode)
	fmt.Printf("E.164 subscriber-number: %s\n", subscriberNumber)

}
Output:
E.164 country-code: 1
E.164 national-destination-code: 604
E.164 subscriber-number: 5551234
Example (Us)
package main

import (
	"fmt"

	"github.com/reiver/go-e164"
)

func main() {

	phoneNumber := "+12061234567"

	countryCode, nationalDestinationCode, subscriberNumber, err := e164.ParseTolerantly(phoneNumber)
	if nil != err {
		fmt.Printf("ERROR: %s\n", err)
		return
	}

	fmt.Printf("E.164 country-code: %s\n", countryCode)
	fmt.Printf("E.164 national-destination-code: %s\n", nationalDestinationCode)
	fmt.Printf("E.164 subscriber-number: %s\n", subscriberNumber)

}
Output:
E.164 country-code: 1
E.164 national-destination-code: 206
E.164 subscriber-number: 1234567

func ParseWithIsoCountryCodeTolerantly

func ParseWithIsoCountryCodeTolerantly(isoCountryCode string, value string) (countryCode string, nationalDestinationCode string, subscriberNumber string, err error)

ParseWithIsoCountryCodeTolerantly parses `value` for a E.164 phone-number (without the country-code) tolerantly, such that it allows spaces between the country-code, the national-destination-code and the subscriber-number. The country-code implied by the ISO 3166-1 alpha-2 country-code,

The basic format is:

[area code][local phone number]

For example:

var countryCode string = "CA"
var phoneNumber string = "6045551234"

countryCode, nationalDestinationCode, subscriberNumber, err := e164.ParseTolerantly(phoneNumber)
// countryCode == "1"
// nationalDestinationCode == "604"
// subscriberNumber == "5551234"

Types

This section is empty.

Jump to

Keyboard shortcuts

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