From PHP to Go: What Took Me Longest to Rewire
I wrote PHP for seven years. I used Laravel for five of them. When I moved to Go to lead a migration from a Laravel monolith to microservices, I did not come with a tutorial. I came with a decade of PHP habits.
Learning the syntax was easy. You can read Go in an afternoon. The hard part was unlearning how I thought about code.
Here are the mental shifts that took me months to master:
Error Handling In PHP, I used try/catch. Errors often traveled invisibly to a global handler. In Go, errors are values. A function returns an error as its last value. You must handle it right there. At first, this felt like extra work. Later, I realized it makes failure visible. When a service fails, the error message acts as a breadcrumb trail. It tells you exactly where the failure happened without a massive stack trace.
Memory and State PHP uses a shared-nothing model. Every request starts with a clean slate. Memory leaks matter less because the process dies after the request. Go is different. The process stays alive for thousands of requests. This means a package-level variable is shared across every request. If two requests try to write to a map at the same time, the whole program crashes. In Go, you own the concurrency. You must use tools like the race detector to keep things safe.
The Role of Context In PHP, the request is the boundary. When the request ends, everything stops. In Go, nothing stops automatically. If a client disconnects, your goroutines might keep running and wasting resources. You must use context.Context to pass cancellation signals through your code. It is the spine that keeps your service healthy under heavy load.
Composition Over Inheritance Laravel relies heavily on extending base classes. You add features by inheriting behavior. Go does not have class inheritance. It uses interfaces that are satisfied implicitly. You define what you need where you use it. This makes code easier to test and swap. I had to kill my instinct to extend everything to write good Go.
Clarity Over Cleverness PHP allows for magic methods and dynamic typing. You can write clever, expressive code. Go is deliberately boring. The compiler forces you to use standard formatting and prevents unused variables. At first, this felt restrictive. Now, I value it. Go optimizes for the person reading the code, not the person writing it. Boring code is easy to fix at 2 AM during an incident.
The hard part of learning a new language is not the new syntax. It is the old assumptions you carry with you.
Source: https://dev.to/econ__11/from-php-to-go-what-took-me-longest-to-rewire-2nfn
