Search
left arrowBack
Pavel Rykov

Pavel Rykov

March 27, 2023 ・ Code

Switching to GoLang from PHP (1 of 3)

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.

  • Code