fastpostcode
A fast postcode validator written in Go. Capable of validating every postcode in the United Kingdom in milliseconds.
The regular expressions for the matcher are from this stackoverflow answer.
These were rewritten as ragel expressions and compiled into a Finite State Machine.
The trade-off for this performance is binary size, with the matcher being ~1600 lines of code. If you want an idiomatic Go implementation, the regex version is here.
Validating a postcode by its pattern catches invalid inputs, but there will be false positives. Validator is tested against all ~1.8 million postcodes.
See
Benchmarks
Benchmark_IsValid/fastpostcode_Valid 111773841 10.7 ns/op
Benchmark_IsValid/fastpostcode_Invalid 150052951 7.82 ns/op
Benchmark_IsValid/regex_Valid 4851324 222 ns/op
Benchmark_IsValid/regex_Invalid 714759 1516 ns/op
Example
package main
import (
"fmt"
"github.com/T-J-L/fastpostcode"
)
func main() {
if fastpostcode.IsValid("SW1A 1AA") {
fmt.Println("This looks like a valid postcode")
}
}
License
fastpostcode is released under the MIT License.