ivinco
Switching to GoLang from PHP (1 of 3)

Switching to GoLang from PHP (1 of 3)

Pavel RykovPavel Rykov··Updated

In this tutorial series, we will take a look at how to convert an existing Laravel CRUD API to GoLang while maintaining its functionality. We will start with a simple PHP application built using the Laravel framework and gradually transition to a GoLang equivalent.

Introduction

GoLang, also known as Go, is an open-source programming language developed by Google. It is designed to be simple, efficient, and reliable, which makes it an excellent choice for developing web applications and APIs. While PHP has been a popular choice for web development over the years, GoLang offers several advantages, such as improved performance, concurrency, and ease of deployment.

In this tutorial, we will take an existing Laravel CRUD API and convert it to a GoLang application. The API is built on the Laravel framework and interacts with the "users" table in the database. We will walk through the process of setting up a GoLang environment, creating the project structure, defining models, implementing handlers, and configuring routing and middleware. By the end of this series, you will have a solid understanding of how to transition from PHP to GoLang for web development.

In the next chapter, we will set up a new project using Laravel framework.

Overview of the Laravel API Example

In this chapter, we will create a simple CRUD (Create, Read, Update, Delete) API application using the Laravel PHP framework, working with the "users" table.

The available endpoints for CRUD API will be like:

  • GET /api/users - Retrieve all users

  • POST /api/users - Create a new user

  • GET /api/users/{id} - Retrieve a single user by ID

  • PUT /api/users/{id} - Update a user by ID

  • DELETE /api/users/{id} - Delete a user by ID

This will serve as a starting point for developers looking to transition from PHP to GoLang.

We will not cover migration or frontend development, as this is meant to be an API solution.

Create a new Laravel project

Run the following command to create a new Laravel project:

composer create-project --prefer-dist laravel/laravel php-crud-api

Change into the project directory:

cd php-crud-api

Define the User model and resource

In the app/Models directory, you'll find a default User.php model. We will use it for our CRUD operations.

Next, create a new API resource using the following command:

php artisan make:resource UserResource

This will generate a UserResource.php file in the app/Http/Resources directory. Open this file and modify the toArray() method to return the user's data:

public function toArray($request)
{
    return [
        'id' => $this->id,
        'name' => $this->name,
        'email' => $this->email,
        'created_at' => $this->created_at,
        'updated_at' => $this->updated_at,
    ];
}

Create the UserController

Run the following command to generate a new controller:

php artisan make:controller UserController --api

This will create a UserController.php file in the app/Http/Controllers directory.

Implement CRUD methods in UserController

Open the UserController.php file and add the following CRUD methods:

<?php

namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Http\Request;
use App\Http\Resources\UserResource;

class UserController extends Controller
{
    public function index()
    {
        return UserResource::collection(User::all());
    }

    public function store(Request $request)
    {
        $user = User::create($request->all());

        return new UserResource($user);
    }

    public function show(User $user)
    {
        return new UserResource($user);
    }

    public function update(Request $request, User $user)
    {
        $user->update($request->all());

        return new UserResource($user);
    }

    public function destroy(User $user)
    {
        $user->delete();

        return response()->json(null, 204);
    }
}

Register API routes

Open the routes/api.php file and add the following route:

use App\Http\Controllers\UserController;

Route::apiResource('users', UserController::class);

Test the API

You can now test your CRUD API using tools like Postman or curl. Run the development server:

php artisan serve

Now that you have a basic understanding of how to create a Laravel CRUD API, you can interact with the application using the provided endpoints. With this foundation in place, you are now ready to explore the process of converting the application to GoLang.

In the upcoming chapters of this tutorial series, we will focus on setting up a GoLang environment, creating a GoLang web server, implementing the CRUD functionality, and finally, testing and benchmarking the resulting API.

Stay tuned for the next article in this series, where we will dive into the world of GoLang, taking our first steps towards transitioning from PHP to a more powerful and efficient programming language.


Planning to move a PHP app to Go? Our Go development team can take it on, and the code we write under contract belongs to you. We have run production systems since 2009, and the first conversation is free.

Frequently Asked Questions

Why migrate from PHP Laravel to Go for a REST API?

Go compiles to a single binary with no runtime dependencies, handles concurrency natively via goroutines, and typically outperforms PHP for CPU-bound API workloads. Laravel is excellent for rapid prototyping, but under sustained high load, the overhead of PHP-FPM and the framework becomes measurable. Migration pays off when horizontal scaling costs start outpacing the migration effort.

How long does it take to migrate a Laravel CRUD API to Go?

A simple CRUD API like the one in this series is the quick case. The complexity comes from replicating Laravel's implicit behaviors — validation, error formatting, middleware chaining — which require explicit code in Go. A production-grade migration adds tests, and a full Laravel application with authentication, queues, and third-party integrations takes considerably longer.

Do you need a Go web framework, or can you use the standard library?

For a simple CRUD API, Go's net/http standard library is sufficient. For projects that need routing parameters (e.g., /users/{id}), middleware chains, and request binding, chi or gin are good choices: both are lightweight and add minimal overhead. Avoid heavy frameworks like Beego for API-only services, since they import functionality you'll never use.

How does Go handle database interactions compared to Laravel Eloquent?

Go has no ORM with the same ergonomics as Eloquent. Use sqlx for struct scanning with raw SQL, or gorm when the team prefers an ORM-style interface. The main difference is that SQL errors are explicit in Go — there's no magic hydration, and every query result must be checked. This verbosity is a feature in production: it makes database failure paths obvious and forces you to handle them.

What is the biggest challenge when switching from PHP to Go?

Error handling. PHP throws exceptions that bubble up automatically; Go returns errors as values that must be explicitly checked at every call site. This feels verbose initially, but it eliminates an entire class of silent failures. PHP developers should make a habit of writing if err != nil checks immediately after every function call. Once that pattern is habitual, the rest of Go feels straightforward.