ISO 3166-1 alpha-2 Country Code in GO
Validate 2-letter ISO 3166-1 alpha-2 country codes (US, GB, FR, JP, etc.) — structural check only.
Try it in the GO tester →Pattern
regexGO
^[A-Z]{2}$Go (RE2) code
goGo
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`^[A-Z]{2}$`)
input := `US`
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
^[A-Z]{2}$ requires exactly two uppercase letters anchored to the full string. Like the currency-code pattern, this verifies FORMAT not membership — combine with the official list to ensure the code actually exists.
Examples
Input
USMatches
US
Input
GBMatches
GB
Input
USANo match
—