go-migrationgo-migration
Factories
Documentation

Faker Methods

Reference table of all available Faker methods for generating realistic test data in factories.

Faker Methods

The factory.Faker instance passed to your factory builder provides methods for generating a wide range of realistic random data. Each call produces a new random value.

Method Reference

MethodReturn TypeDescription
f.Name()stringFull person name (e.g., "Alice Johnson")
f.FirstName()stringFirst name (e.g., "Alice")
f.LastName()stringLast name (e.g., "Johnson")
f.Email()stringEmail address (e.g., "alice@example.com")
f.Phone()stringPhone number
f.Address()stringFull street address
f.City()stringRandom city name
f.Country()stringRandom country name
f.UUID()stringRandom UUID v4 string
f.Sentence()stringRandom sentence of text
f.Paragraph()stringRandom paragraph of text
f.Word()stringRandom single word
f.Bool()boolRandom boolean value
f.IntBetween(min, max)intRandom integer in range [min, max]
f.Float64Between(min, max)float64Random float in range [min, max)
f.Date()time.TimeRandom date
f.DateBetween(start, end)time.TimeRandom date between start and end
f.Pick(items)stringRandom element from a string slice

Usage Examples

Personal Information

go
factory.NewFactory(func(f factory.Faker) Contact {
    return Contact{
        Name:    f.Name(),
        Email:   f.Email(),
        Phone:   f.Phone(),
        Address: f.Address(),
    }
})

Numeric Values

go
factory.NewFactory(func(f factory.Faker) Product {
    return Product{
        Price:    f.Float64Between(1.00, 999.99),
        Stock:    f.IntBetween(0, 500),
        Rating:   f.Float64Between(1.0, 5.0),
    }
})

Text Content

go
factory.NewFactory(func(f factory.Faker) Article {
    return Article{
        Title:   f.Sentence(),
        Body:    f.Paragraph(),
        Slug:    f.UUID(),
    }
})

Identifiers and Booleans

go
factory.NewFactory(func(f factory.Faker) Account {
    return Account{
        ExternalID: f.UUID(),
        Active:     f.Bool(),
        CreatedAt:  f.Date(),
    }
})

Every call to a Faker method produces a new random value. Two calls to f.Name() within the same builder will return different names.

Complete Example

factories/order_factory.go
package factories

import (
    "time"

    "github.com/gopackx/go-migration/seeder/factory"
)

type Order struct {
    ID        string
    Customer  string
    Email     string
    Total     float64
    Items     int
    Shipped   bool
    CreatedAt time.Time
}

var OrderFactory = factory.NewFactory(func(f factory.Faker) Order {
    return Order{
        ID:        f.UUID(),
        Customer:  f.Name(),
        Email:     f.Email(),
        Total:     f.Float64Between(10.00, 500.00),
        Items:     f.IntBetween(1, 20),
        Shipped:   f.Bool(),
        CreatedAt: f.Date(),
    }
})

What's Next?