faker

Struct-tag-driven fake data generator for Go, turning any struct into realistic test fixtures with one function call.

Library
Go
vv4.11.0
861stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
63/100Good
Development Activity56
Maintenance52
Community48
Maturity56
Momentum40

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
74/100Good
Architecture78
Code Quality85
Innovation78
Learning Curve55

Faker is a Go library that populates arbitrary structs with realistic fake data by reading faker struct tags via reflection, similar to how encoding/json reads json tags. A single faker.FakeData(&target) call walks the struct, recognizes tags like first_name, email, uuid_hyphenated, cc_number, and dozens of others, and fills each exported field accordingly — including nested structs, slices, maps, and pointers.

Beyond basic field-level generation, it supports advanced tag options: unique values with automatic retry, oneof enumerated choices, template tags that derive one field’s value from others via Go templates, configurable length/boundary constraints, multi-language name/locale data, and depth limits to safely handle recursive or deeply nested struct graphs. Custom tag providers can be registered with AddProvider to extend the generator with project-specific fake data types.

What You Get

  • Tag-driven field generation - Struct tags like first_name, email, uuid_hyphenated, and cc_number map directly to typed fake-data generators for common real-world fields.
  • Unique value tracking - The unique tag retries generation up to a configurable limit to guarantee no duplicate values across a run.
  • Template-derived fields - The template tag lets one string field reference and interpolate other fields on the same struct using Go’s text/template syntax.
  • Custom provider registration - AddProvider/RemoveProvider let you register your own tag handlers or override built-in time.Time and other struct-type providers.
  • Depth and size limits - MaxDepthOption and slice/map size boundaries prevent runaway generation on recursive or deeply nested struct graphs.

Common Use Cases

  • Unit and integration test fixtures - Generate realistic struct instances for table-driven Go tests without hand-writing sample data.
  • Database seeding - Populate development or staging databases with plausible records (names, addresses, emails, phone numbers) for demos.
  • API contract testing - Fill request/response structs with valid-looking fake payloads to exercise serialization and validation paths.
  • Load and benchmark testing - Quickly produce large volumes of varied struct data to stress-test endpoints or pipelines.

Under The Hood

Architecture The library centers on a single reflection-driven entry point, FakeData(a any, opt ...options.OptionFunc) in faker.go, which walks a target struct’s fields via reflect, reads the faker tag on each field, and dispatches to one of dozens of typed generator functions (address.go, person.go, internet.go, datetime.go, payment.go, phone.go, price.go, uuid.go, user_agent.go, lorem.go, blood.go). Cross-cutting concerns are split into pkg/options (thread-safe global configuration behind sync/atomic and unsafe.Pointer, covering unique-value mode, string length, boundaries, and language), pkg/interfaces (shared type contracts for languages, numbers, and custom providers), pkg/errors (centralized error-message constants), and pkg/slice (small collection helpers). Extensibility comes from AddProvider/RemoveProvider, which register custom tag handlers into a shared map, and from tag_argument_extractor.go plus template_tag.go, which parse tag arguments (length, boundaries, template bodies) and evaluate template-tagged fields with cycle detection so one field can safely reference others by name.

Tech Stack A dependency-light, idiomatic Go module (module github.com/go-faker/faker/v4, Go 1.26) built almost entirely on the standard library — reflect, regexp, crypto/rand, math/rand, sync, sync/atomic, and text/template — with a single external dependency, golang.org/x/text, for locale/text-handling support. There is no runtime framework or database; the project ships as a pure library plus its test suite. Tooling is Make-driven: gotestsum and tparse for structured test output, golangci-lint (v2 config) for linting, and GitHub Actions (go.yml) running the matrix against Go ~1.26 with GOTOOLCHAIN=local pinning, plus a separate coverage job uploading to Codecov with the race detector enabled.

Code Quality Every core source file has a matching test file of comparable or greater size (e.g. an 82KB faker_test.go alongside a 45KB faker.go, a 24KB template_tag_test.go alongside template_tag.go), and a dozen example_*_test.go files double as runnable documentation for each tag family. CI runs tests with -race and enforces a broad golangci-lint rule set — errcheck, staticcheck, revive, govet, gocyclo, unused, ineffassign, unconvert, unparam, misspell, goconst — with narrowly scoped exclusions rather than blanket suppressions. Error handling is centralized as exported string/format constants in pkg/errors rather than scattered inline strings, and exported tag names are declared as named constants in faker.go instead of magic strings, giving the public tag surface a single source of truth.

API Design The public surface deliberately mirrors encoding/json’s tag convention (faker:"email" next to a struct field, one FakeData(&target) call to populate it), so idiomatic Go developers get near-zero onboarding friction. Beyond that minimal happy path, the tag grammar layers in real ergonomics for harder cases: unique for collision-free values, oneof for enumerated choices, boundary_start/boundary_end and len/slice_len for constrained ranges, lang for locale selection, and template for fields derived from sibling fields — all without requiring callers to write generator code themselves. Extensibility (AddProvider, StructTypeProviders) is opt-in and additive, so the common case stays a single function call while advanced users can still customize generation for project-specific types.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search