Go Playbook

Go 1.27 · for PHP and Laravel developers

Go in 6 weeks, for Laravel developers

Learn to read and write Go, build real command-line tools, and plug Go into a Laravel app two ways: a worker that takes jobs from the database Laravel fills, and a net/http service Laravel calls over HTTP. Then build the same service on Laravel Go, Laravel’s own framework for Go. One small concept at a time, every idea compared with the PHP you already know.

Start here

1. One conceptExplained in plain words, then shown in a small, complete program.
2. The compiler says noThe mistake a PHP developer makes, with the real error the go command prints.
3. Coming from PHPThe same idea in PHP, side by side, so you know where it fits.
4. PracticeA Tour of Go, Learn Go with Tests, and a short exercise with the solution folded.

The plan

2 to 3 hours a day. Days 1–5 teach, Day 6 builds the week’s tool, Day 7 is the checkpoint. Every program on this site was built with Go 1.27.4, and every error and output is what the go command really printed. 42 of 42 days are published.

0Day 0: Set up GoInstall Go 1.27, an editor that understands Go, and the two free companions. 30 minutes.Read

Week 1Basics and a first CLIbuilds logstat v1

The go command, variables and zero values, functions that return errors, slices, maps and structs. You finish with a CLI that reads a real laravel.log.

  1. 1The go command and your first programCreate a module, run and build it, and read go.mod the way you read composer.json.10 min
  2. 2Variables, types and zero valuesOne fixed type per variable, a zero value instead of null, conversions on purpose, and if and for.20 min
  3. 3Functions and errorsFunctions that return two values, and errors as ordinary values you check with if err != nil.20 min
  4. 4Slices and mapsThe list and the lookup table you will use every day, range over both, and the nil traps.20 min
  5. 5Structs and switchYour own types: a struct with fields, a slice of structs, a named type with constants, and switch.20 min
  6. 6Build logstat v1Build dayA CLI that reads a laravel.log and counts entries by level, using only what this week taught.15 min
  7. 7Week 1 checkpointCheckpointFive broken programs to fix and explain. Pass them all before Week 2.20 min

Week 2Methods, interfaces, errors, testsbuilds logstat v2

Pointers and methods, interfaces nobody has to declare, wrapped errors, packages and table tests. logstat gets real flags and clean errors.

  1. 1Pointers and methodsChange a value through a pointer, and give a type methods, with a value or a pointer receiver.20 min
  2. 2InterfacesAn interface lists methods, and any type that has them satisfies it, without saying so.20 min
  3. 3Wrapping and inspecting errorsAdd context with %w, check for a known error with errors.Is, and read a custom error with errors.As.20 min
  4. 4Packages and project layoutSplit a program into packages, export with a capital letter, and keep internal code internal.10 min
  5. 5Tests with go testA test is a function in a _test.go file, and a table of cases covers the edges in a few lines.10 min
  6. 6Build logstat v2Build dayReal flags with the flag package, errors with context, a parser in its own package, and tests for it.35 min
  7. 7Week 2 checkpointCheckpointgo vet and staticcheck clean, every error checked, and every failure ends in a clear message.20 min

Week 3Real data and speedbuilds csv2json

JSON with struct tags, streaming CSV, structured logging, generics and the slices package. You benchmark a Go tool against the same thing in PHP.

  1. 1JSON and struct tagsTyped json_encode and json_decode: struct tags name the fields, and unknown or missing fields are your call.25 min
  2. 2Streaming CSVRead a huge CSV one row at a time, and report bad rows instead of stopping.25 min
  3. 3Structured logging with slogLog lines with fields, as text or JSON, on stderr, at the level you choose.10 min
  4. 4GenericsWrite a function once for many types, with type parameters and constraints.15 min
  5. 5Closures and the slices packageFunctions as values, what a closure captures, and sorting and searching with slices and maps.20 min
  6. 6Build csv2jsonBuild day100K survey rows to JSON Lines with validation, benchmarked against PHP.40 min
  7. 7Week 3 checkpointCheckpointFlat memory on a 1M-row file, and bad rows never stop the run.20 min

Week 4Concurrency and plugging into Laravelbuilds laravel-go-worker

Goroutines, channels, context, an HTTP client and Postgres, and a Go worker that processes what Laravel queues, with a limit on how much runs at once.

  1. 1Goroutines and WaitGroupRun work at the same time with go, and wait for all of it with a WaitGroup.20 min
  2. 2Channels and selectSend values between goroutines, close a channel to say “done”, and wait on several with select.15 min
  3. 3context and the HTTP clientCancel work and set deadlines with context, and call APIs with timeouts and status checks.30 min
  4. 4Postgres with database/sql and pgxA connection pool, queries with placeholders, scanning rows into structs, and transactions, on your Laravel database.45 min
  5. 5errgroup, worker pools and the race detectorRun many tasks with a limit, stop all of them on the first error, and catch data races with -race.25 min
  6. 6Build the Laravel workerBuild dayAn artisan command queues a batch, the Go worker processes it concurrently and writes results in one transaction.1 h 5 min
  7. 7Week 4 checkpointCheckpointKill the database mid-run: Laravel gets a clear error and no half-written batch.35 min

Week 5HTTP services with net/httpbuilds laravel-go-service

Routing, JSON and validation, middleware, errors as responses and tests with httptest, all from the standard library. Laravel calls it with Http::post().

  1. 1Routing and handlersAn http.ServeMux with methods and path parameters in its patterns, and handlers.15 min
  2. 2JSON in and out, with validationDecode request bodies into structs, validate them like a Form Request, and answer 422.20 min
  3. 3Shared dependencies and errors as responsesHandlers as methods on a struct that holds the pool, and one place that turns errors into status codes.15 min
  4. 4MiddlewareWrap handlers: logging, an API key check, timeouts and panics recovered.15 min
  5. 5Tests and graceful shutdownTest handlers with httptest, and stop the server without dropping requests.20 min
  6. 6Build the serviceBuild dayPOST a batch, poll its status, and Laravel calls it with Http.1 h 35 min
  7. 7Week 5 checkpointCheckpointIntegration tests against a fresh database, and the service’s exit check.20 min

Week 6Laravel Gobuilds the service on Laravel Go

Laravel’s own framework for Go (beta, v0.1.1), built on the net/http of Week 5: routes, typed validation, models, queues, workflows and tests, the Laravel way.

  1. 1Laravel Go: install and the shape of an appThe CLI, a new application, and every folder next to the PHP Laravel one it stands for.20 min
  2. 2Routes, middleware and validationRoute files and groups, middleware, and validation on a typed struct.35 min
  3. 3Models and migrationsModels as plain structs, a query builder with generics, relations and migrations in Go.45 min
  4. 4Queues and tasksJobs as structs with a Handle method, retries, and several queries at once with the task package.25 min
  5. 5WorkflowsA long process written as one method: each step runs once, and the flow survives restarts.30 min
  6. 6Build the service on Laravel GoBuild dayThe Week 5 analysis service again: the queue takes over the background work, the rules stay yours.55 min
  7. 7Tests and the Week 6 checkpointCheckpointTests through the full HTTP stack, and when to pick net/http, Laravel Go or PHP Laravel.40 min