International Phone (E.164) in GO
Validate phone numbers in ITU-T E.164 international format: a + sign followed by 2–15 digits, first digit non-zero.
Try it in the GO tester →Pattern
regexGO
^\+[1-9]\d{1,14}$Go (RE2) code
goGo
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`^\+[1-9]\d{1,14}$`)
input := `+14155552671`
for _, match := range re.FindAllString(input, -1) {
fmt.Println(match)
}
}Uses `regexp.MustCompile` (panics on bad patterns at startup) and `FindAllString` for all matches.
How the pattern works
^ anchors the start. \+ matches the required leading plus sign. [1-9] ensures the country code starts with a non-zero digit. \d{1,14} allows 1 to 14 additional digits for a total of 2–15 digits after the +, per E.164 spec. $ anchors the end.
Examples
Input
+14155552671Matches
+14155552671
Input
+442071838750Matches
+442071838750
Input
14155552671No match
—