zipper

package module
v0.2026.42 Latest Latest
Warning

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

Go to latest
Published: Apr 8, 2026 License: CC-BY-4.0, MIT Imports: 9 Imported by: 0

README

zipper

A zero-dependency Go library for US zip code lookups, powered by embedded GeoNames postal code data.

Install

go get github.com/bmorton/zipper

The module version follows CalVer (v0.YYYY.MMPATCH) to track data vintage. Upgrade to get fresher data; the API is stable.

Usage

package main

import (
    "fmt"
    "github.com/bmorton/zipper"
)

func main() {
    // Look up a zip code
    e := zipper.LookupOne("10001")
    fmt.Printf("%s, %s %s\n", e.City, e.StateCode, e.ZipCode)
    // Output: New York, NY 10001

    // Find the nearest zip to coordinates
    nearest := zipper.Nearest(40.748, -73.985)
    fmt.Printf("Nearest: %s %s\n", nearest.City, nearest.ZipCode)

    // Find 5 nearest zip codes
    nearby := zipper.NearestN(40.748, -73.985, 5)
    for _, n := range nearby {
        fmt.Printf("  %s %s\n", n.City, n.ZipCode)
    }

    // Find all zips within 10km
    within := zipper.WithinRadius(40.748, -73.985, 10.0)
    fmt.Printf("Found %d zips within 10km\n", len(within))

    // Dataset info
    fmt.Printf("Data version: %s, entries: %d\n", zipper.DataVersion(), zipper.Size())
}

API

Function Description
Lookup(zip) []Entry All entries for a zip code (nil if not found)
LookupOne(zip) *Entry First entry for a zip code (nil if not found)
Nearest(lat, lon) *Entry Closest zip to coordinates (Haversine)
NearestN(lat, lon, n) []Entry N closest zips, distance-ordered
WithinRadius(lat, lon, km) []Entry All zips within radius, distance-ordered
All() []Entry Every entry in the dataset (do not modify)
DataVersion() string Data vintage, e.g. "2025.04"
Size() int Total number of entries

CLI

A demo CLI is included for quick lookups:

# Build the CLI
make build

# Look up a zip code
./bin/zipper lookup 10001
# ZIP    CITY      STATE          LAT      LON
# 10001  New York  New York (NY)  40.7484  -73.9967

# Find the 3 nearest zips to coordinates
./bin/zipper nearest -n 3 40.748 -73.985
# ZIP    CITY      STATE          LAT      LON
# 10118  New York  New York (NY)  40.7490  -73.9865
# 10120  New York  New York (NY)  40.7506  -73.9894
# 10123  New York  New York (NY)  40.7515  -73.9905

# Find all zips within 5 km
./bin/zipper within 40.748 -73.985 5

# Show dataset info
./bin/zipper info

Data

The embedded dataset is built from two sources:

This means zip codes that span multiple cities (e.g. 94608 → Emeryville + Oakland) correctly return all associated place names.

Data is refreshed monthly via GitHub Actions.

Regenerating Data

make generate

This downloads the latest GeoNames export and Census ZCTA-Place file, merges them, and writes data/zipcodes.csv.gz.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DataVersion

func DataVersion() string

DataVersion returns the data vintage, e.g. "2025.04".

func Size

func Size() int

Size returns the total number of entries.

Types

type Entry

type Entry struct {
	CountryCode string // ISO 3166-1 alpha-2, e.g. "US"
	ZipCode     string
	City        string
	State       string // full name, e.g. "California"
	StateCode   string // abbreviation, e.g. "CA"
	Lat         float64
	Lon         float64
}

Entry represents a single zip code record.

func All

func All() []Entry

All returns every entry in the dataset. The caller must not modify the slice.

func Lookup

func Lookup(zip string) []Entry

Lookup returns all entries for a given zip code. A zip may map to multiple places (e.g. shared zips across cities). Returns nil if not found.

func LookupOne

func LookupOne(zip string) *Entry

LookupOne returns the first entry for a zip code, or nil if not found. Convenience wrapper for the common single-result case.

func Nearest

func Nearest(lat, lon float64) *Entry

Nearest returns the closest zip code entry to the given coordinates using the Haversine formula. Returns nil only if the dataset is empty.

func NearestN

func NearestN(lat, lon float64, n int) []Entry

NearestN returns the N closest entries to the given coordinates, ordered by distance ascending.

func WithinRadius

func WithinRadius(lat, lon float64, radiusKM float64) []Entry

WithinRadius returns all entries within radiusKM kilometers of the given coordinates, ordered by distance ascending.

Directories

Path Synopsis
cmd
generate command
zipper command

Jump to

Keyboard shortcuts

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