Share On: Twitter Facebook Google+ LinkedIn Pinterest Tumblr Reddit

Why did I switch from PHP to Go?

Tags: Golang

There is a programming language that people who have been interested in software for many years especially enjoy writing. This language for me is PHP . Of course, the effect of developing projects with this language for more than 10 years is great, but in recent years, the main reason is that PHP has come to a really good place. For this reason, even though I said I switched to Go in the title of the article, I always believe that I will continue to develop projects with PHP, but for now, this is not our topic, the issue is Go .

I told my story of transitioning from PHP to Go Programming Language at Ankara Gophers June event. First of all, I thought of sharing the presentation as it is (I shared it at the end of the article), but I believed that it would be better to share it as a blog post by adding some of the comments I made verbally during the presentation. I must say from the beginning, the question in the title has a general and a specific answer ;)

General Information About Go

  1. Version 1.0 of Go was released on March 28, 2012, but before that it was used in production in 2009.
  2. It was developed by Ken Thompson, Rob Pike and Robert Griesiemer under the umbrella of Google.
  3. These are the people who designed the Unix operating system, the C programming language, and UTF 8.
  4. It is completely open source. https :// github com golang
  5. It can be compiled and run in Linux, Mac OS X, FreeBSD, NetBSD, OpenBSD and Windows environments.
package main

import "fmt"

func main() {
	fmt.Println("Hello World")
}

Static or Dynamic Type

Variables are defined dynamically in PHP and the type of the variable can change anywhere in the program without an error, depending on the value it receives.

";

$ degisken = 15;
echo gettype($degisken) . "
"; $degisken = true; echo gettype($degisken) . "
"; $ degisken = 15.6; echo gettype($degisken) . "
"; ?>

In Go, variables are defined statically. In other words, you create the variable with a certain type at the beginning and the type of that variable never changes throughout the program. In the picture below, you can see the error I get when I want to assign an integer value to a string type variable .

Object Oriented or Functional Programming

PHP

PHP is an object-oriented language and has inheritance with Classes .

setBrand($brand);
        $this->setPlaka($plaka);
    }
 
    public function showCartGraphics () {
        echo 'Car Make:'.$this->brand." 
Car License Plate:".$this->plate; } private function setBrand($brand) { $this->brand = $brand; } private function setPlaka($plaka) { $this->plaka = $plaka; } } $car1 = new Car("Audi","25 VA 025"); $ car1 -> showCarInfo();
echo "

";
$car2 = new Car("BMV","25 YAKUTER 025");
$ car2 -> showCarInfo();
?>

Go

Go has a functional programming culture, not an object-oriented one. Rather than Class and inheritance, there is Struct and Interface .

package main

import (
	"fmt"
)

type Car struct {
	brand string
	plate string
}

func CarInfo(car Car) (string, string) {
	return car.brand, car.plate
}
func (car *Car) setBrand(newBrand string) {
	car.make = newBrand
}
func (car *Car) setPlate(newPlate string) {
	car.plate = newplate
}
func main() {
	audiCar := Car{
		marka: "Audi",
		plaka: "06 TT 451",
	}
	bmwCar := Car{
		marka: "BMW",
		plaka: "25 YAKUTER 025",
	}

	model, license plate := CarInfo(audiCar)
	fmt.Printf("%s %s \n", model, plaka)

	bmwCar.setPlaka("06 ABK 658")
	bmwCar.setBrand("Anatolia")
model, license plate = CarInfo(bmw Car) fmt.Printf("%s %s \n", model, plaka) }

Hosting Topic

PHP

  • PHP can be easily hosted on all servers such as shared, cloud or physical.
  • There is no need for server control unless it is a large project.

Go

  • Server control is a must in Go.
  • Therefore, a simple web interface (cpanel, plesk, etc.) is not sufficient for hosting.
  • Rather than shared, cloud (cloud) or physical (dedicated) server is needed.

Program Structure and Compilation

PHP

  1. The code written in PHP is compiled instantly and the result is transmitted to the browser instantly.
  2. As code, any PHP file can contain only PHP code, as well as HTML and JS.
  3. Applications emerge with the combination of multiple files.
  4. Dependencies exist as different files on the server.

Go

  1. In Go, the application is compiled from scratch and the compilation results in a single binary file.
  2. All dependencies are collected in the same binary file. Therefore, the file size is larger than normal.
  3. The file cannot be modified later, it must be compiled from scratch.

Package Management, Code Structure, Framework

PHP

  1. PHP does not have a ready-made package manager, Composer is used.
  2. Codes can be written in accordance with patterns, as well as interspersed into dozens of HTML files.
  3. Frameworks (Laravel, Symfony, WordPress) working with MVC logic are preferred for large projects or Framework is written from scratch with additional packages.
  4. Important features such as routing and template are only provided with additional packages.

Go

  1. Go has its own package manager and the desired packages can be easily installed with the "go get ..." command. All dependencies can be easily managed with Go Modules.
  2. It does not allow to write irregular code. In particular, the fmt package provides automatic conversion of the code accordingly.
  3. Tab is used instead of Space.
  4. It has its own routing feature (net/http) and template system.
  5. Thanks to the routing and template system, you can develop very robust and stable projects without using any framework.

General Answer

The comparison I made above is actually quite superficial. Basically, PHP and Go are two completely different languages, both for the purpose of being produced and used. However, if I had to give a brief comparison of my reasons for switching to Go in general:

* I do not need a strong language on the linux system side in the projects I am working on,
* I can say that I develop a product that should be strong on the system, network and web side at the same time.

Of course, these two reasons are what you expected to hear in the first stage after what I wrote above, but my main reason for passing is very different :)

Special Answer


What I wrote so far was about PHP/Go comparison and why I switched to Go in line with general criteria. Let's get to the main issue.

On February 6, 2018, SpaceX launched the Falcon Heavy 9 rocket into space. From software to hardware, from engineering to electronics, this launch was a feast. Personally, I was fascinated by this event with my whole being. However, I felt the need to question myself and all the work I had done until then.

This state of depression continued for a while. I wanted to imagine myself in the team that developed that rocket, but there was a problem. While I have been developing projects with PHP for more than 10 years, was PHP used in such a rocket and did I have a chance to contribute to this project in some way? I decided to ask the question in my mind to the expert of this business, namely Rasmus Lerdorf , the inventor of PHP language . 

 

 

With the answer from him, boiling water was poured over my head.

In summary, I asked him if it was possible to take part in such a project with PHP and I got a response from him that it was not possible :/ After that moment, I decided to switch to a new language. Of course, PHP is a complete language in accordance with its intended purpose (web development), but the things I was going to produce had changed direction.

 

As a result of my research , I learned that Python , C++ and Go are mostly used in Falcon Heavy 9 rocket. At that time, I wasn't very familiar with Python and C++, but I had developed small projects out of my curiosity with Go. That being the case, I chose Go without thinking first.

Because I wanted to code rockets!

Comments(3)

avatar of commentor6
May 10th 2022, 10:44 pm

avatar of commentor7
May 10th 2022, 10:44 pm

avatar of commentor8
May 10th 2022, 10:45 pm