Skip to content
← All work

07 · League simulator in Go

go-football-core

A football season, simulated week by week, with Monte Carlo title odds.

A league simulator written to get comfortable with Go. It plays a season week by week, scores each match from team strength and a morale that rises and falls with results, keeps the table up to date, and — once four weeks are in — predicts the champion by replaying the rest of the season a thousand times.

A layered Fiber API with GORM over SQLite, and the prediction engines behind one interface so a default estimate and a Monte Carlo run can be swapped with a query parameter. Small on purpose.

Year
2025
Role
Backend
Stack
Go 1.24, Fiber, GORM, SQLite
Languages
  • Go69.8%
  • HTML30.2%

From the repository

REST endpoints
9
Prediction strategies
2
Simulated seasons per prediction
1,000
Domain models
4

Technical architecture

A layered Go 1.24 service on Fiber, with GORM over a single SQLite file. Routes hand off to controllers, controllers to services, and only services touch the models.

  1. L1

    Client

    • index.html
    • fetch()

    A single static page that calls the API and draws the table, fixtures and odds.

  2. L2

    HTTP

    • Fiber v2
    • routes.go
    • 5 controllers

    League, match, standing, simulation and prediction controllers; thin, they only parse and respond.

  3. L3

    Services

    • Simulator
    • Predictor interface
    • DefaultPredictor
    • MonteCarloPredictor
    • Reset

    Fixture generation, weighted scoring, morale updates and the two interchangeable prediction engines.

  4. L4

    Data

    • GORM
    • SQLite (league.db)
    • Seed

    Teams, matches, standings and season state; a seeder builds the league on first run.

A week of football

What happens when POST /simulate is called.

  1. 01

    Fixtures

    The current week's pairings are generated from the team list.

  2. 02

    Score

    Goals are drawn at random, capped by each side's effective power — strength adjusted by morale.

  3. 03

    Morale

    Winners gain 5–10, losers drop 5–10, a draw moves both by ±2; morale is clamped between 25 and 100.

  4. 04

    Predict

    After week four, GET /predict replays the rest of the season 1,000 times and returns each team's title odds.

API examples

Response shapes are taken from the controllers; the team names are the seeded league, the numbers are examples.

POST/simulate

Plays the current week.

Response

{ "success": true, "message": "Week 5 simulated." }

GET/predict?method=montecarlo

Replays the rest of the season 1,000 times.

Response

{
  "success": true,
  "prediction_week": 5,
  "prediction": [
    { "team": "Liverpool",       "chance": 48 },
    { "team": "Arsenal",         "chance": 29 },
    { "team": "Manchester City", "chance": 16 },
    { "team": "Chelsea",         "chance": 7 }
  ]
}

// before week four
{ "prediction_week": 0, "error": "At least 4 weeks must be played before making predictions." }

Engineering decisions

  • Strategies behind an interface

    Both predictors satisfy one Predictor interface; the controller picks one from a query parameter and nothing else changes.

  • Morale for play, strength for odds

    Simulation uses morale so streaks feel real; prediction deliberately ignores it so the odds are not swayed by last week.

  • Monte Carlo with variance

    The Monte Carlo engine multiplies strength by a random factor between 0.5 and 1.5 per match, which lets upsets happen at a believable rate.

Engineering checklist

The questions a reviewer asks, answered from the code — including the ones where the honest answer is no.

Database designIn place
Four GORM models in SQLite with AutoMigrate; matches reference both teams by foreign key, and the standings are recomputed from matches rather than edited by hand, so the table can never disagree with the results.
Authentication & authorizationNot applicable
None: a single-user simulator with no accounts.
API & data securityPartly
Input is validated in the controllers (for example, predictions refuse to run before week four), but CORS is open to every origin.
PerformancePartly
A thousand seasons per prediction run in memory on copies of the standings, without touching the database.
Errors & loggingIn place
Every error path returns a JSON body with a status code — 400 for bad input, 404 for a missing match, 500 for storage errors.
TestsNot yet
No _test.go files yet; the predictors are the obvious first target.
Deployment & CI/CDNot yet
Runs locally on :8080.

What I would change now

  • Seeding a local rand.Rand per run, instead of the global seed, would make simulations reproducible and testable.
  • The 1,000 seasons are independent — a natural place for a worker pool of goroutines.

Next project

Ticket Reservation System

Customers reserve, managers report — all in the terminal.