DEV Community

Boris Jamot ✊ /
Boris Jamot ✊ /

Posted on • Updated on • Originally published at mamyn0va.github.io

My Journey from PHP to Go

As an introduction, let me tell you that I've been using Go for only one week. But I think that a newcomer's opinion can be valuable for the community and other newbies.

Let me also tell you that I'm not intending to quit PHP. It's my main language. But I need to learn a new language, to change of paradigm, and Go is the one I'm most interested in.

It's simple, close to the system, and really suited for cloud based applications. Although some frameworks exists for Web development, it's not the philosophy of Go.

It's a language for craftsmen!

Building my own (dis)Integrated Development Environment

As an experienced (PHP) developer, my goal is to find a way to be as productive in Go as in PHP. Thus, I'm trying to build an efficient development environment. Here are the basics:

  • an editor with linting, syntax highlighting, code formatting, code browsing, an outline view, a tree view
  • a term with a unit tests watcher
  • a term with a running instance of my app
  • a term to work with git

Regarding the editor, as an aficionado of Atom, I first gave it a try. The plugins go-plus and ide-go seemed interesting, but unfortunately I faced an issue that prevent me to use it.
So I tried Intellij Ultimate with the Go plugin, but I had a permission issue while trying to build, run or test my app.
Finally, I ended by using my good old vim with vim-go plugin. That's the only one which worked directly out-of-the-box, without any configuration.

Then, I used tmux+tmuxp to create a custom layout with vim and 3 terms for the unit tests, the running app and the git commands.

Organizing my project

The next step is to bootstrap a project from scratch and to set the foundations for a maintainable codebase. My future app is a fairly straightforward database driven API that consumes other API. So with Go I should end up with a unique executable.

I am used to split my code as follows:

  • a controller folder
  • a business folder
  • a model folder (aka DTO)
  • a mapper folder (aka DAO)
  • optionally a common folder for shared stuff
  • an index.php file

For the moment, I intend to use the same organization for my Go project but I'm a bit confused by the fact that Go files are just a way to split the code but not to isolate it from the rest of the code. What I mean is that in PHP or any other OOP language, if I put a class in a file, it gets isolated from other classes (except public properties & functions). In Go, if I split my code in two files inside a given folder, it will be re-assembled by the compiler as if it was a single file. Does it mean that I need to create sub-packages for each of my files? I don't think so, but it's too early for me to answer.

Composer VS Go modules

While PHP offers a great dependency manager (composer) since 2012, my first impression is that it's not that simple with Go.

I heard about godep but it's a third-party tool and it doesn't seem to be as widely adopted as Composer for PHP (maybe I'm wrong). A colleague of mine who is highly experienced with Go told me about Go modules. It's still experimental in 1.11 but it should be ready in 1.12, so I decided to give it a chance.

Paradigm shifting

I've been an OOP developer (PHP, Java) for 15 years now, so switching to a language like Go is not easy. The most difficult is to forget all about classes and objects. Instead, I can use structs and interfaces. At first it was troubling, but after few days, I'm getting more and more used to that. And it's even a good thing to get rid of constructors, getters and setters.

One other breaking change is about the asynchronous processing. In traditional PHP, all is processed synchronously. The drawback is that there is no parallelism, and that you can waste a lot of time waiting for I/O, but the advantage is that it makes your code more readable and straightforward. In Go, like in other asynchronous languages like JavaScript, you take advantage of all the resources of your host by doing certain things while some others are waiting, for example, for an API response.

Thus, I'm beginning to play with goroutines and channels...

Picking up the right libraries

Finally, I had to find some libraries to help me with some basical stuff:


Here it is! I'll post a new article in few months when I'll have some more experience and feedbacks to give.

Any suggestion is welcome for the newbie that I am :)

Thanks for reading.

Top comments (15)

Collapse
 
rhymes profile image
rhymes

Hi Boris, nice that you're trying something new! As a lot of people say, learning a new language probably will improve your skills at your "first" language even if you don't use the new one during your daily work.

For the moment, I intend to use the same organization for my Go project but I'm a bit confused by the fact that Go files are just a way to split the code but not to isolate it from the rest of the code.

It depends on how you organize your code.

If two files belong to the same package then yes, the splitting is just logic, but they are technically the same unit of work (package) to begin with.

The convention is to name the package with the same name of the directory those files are in. So if you have a directory "store", all the files inside it should belong to the store package. You should also avoid mixing files from different packages in the same directory.

What I mean is that in PHP or any other OOP language, if I put a class in a file, it gets isolated from other classes (except public properties & functions). In Go, if I split my code in two files inside a given folder, it will be re-assembled by the compiler as if it was a single file. Does it mean that I need to create sub-packages for each of my files? I don't think so, but it's too early for me to answer.

Encapsulation in Go is pretty straightforward. In each package, everything whose name starts with a lowercase letter is private to that package, everything with a uppercase letter is visible to other packages, and can be imported.

So func privateFunction() {} will be accessible only inside your package but func PublicFunction() {} can be used in other packages.

To organize a Go project I googled a bit but ended up copying how Go projects on Github organize their code (and failed to adhere in a way). There's no single way but popular Go projects on GitHub are as good a starting point as any.

I've been an OOP developer (PHP, Java) for 15 years now, so switching to a language like Go is not easy. The most difficult is to forget all about classes and objects.

A note: there's not a single way to do OOP, the class based paradigm popularized by languages like Java is definitely the most famous known but you can do OOP in many different ways. Strictly speaking OOP is about objects (and their relationships), not about classes. You need to have objects, with a state and with methods that can operate on such state and alter it. Objects can inherit behavior and state from one another. JavaScript is probably the most famous example of OOP done without classes (well, until ES6 but if I remember correctly that's just syntax on top of prototypes).

Go has taken some concepts from OOP: you have methods that operate on structs that hold data. There's no direct inheritance, though you can use composition to build a new struct from another one (and "inherit" state and methods).
Polymorphism is achieved through interfaces.

So, is Go OOP? Probably. The official FAQ has this to say:

go oop

Thus, I'm beginning to play with goroutines and channels...

Have fun, remember that concurrency is not parallelism...

If you're interested in my initial experience with Go I documented it here:

Collapse
 
biros profile image
Boris Jamot ✊ /

Thanks a lot for your detailed answer!
I'll take a look at your post.

I knew about functions' visibility with upper/lower case and I found it was a great way to simplify the code readability.

Collapse
 
itaditya profile image
Aditya Agarwal

I am from Node.js background and gave Go a try recently. After trying out some basic stuff and using Mux etc. I tried Beego Web Framework along with Go Modules. It's really amazing, solves most of my problems.

Collapse
 
biros profile image
Boris Jamot ✊ /

Most of the time, I try to avoid frameworks, even if it could change. But for the moment, I want to learn raw Go as it's the language's philosophy.

Collapse
 
itaditya profile image
Aditya Agarwal

Happy to know your way.

Collapse
 
andrimarin profile image
andrimarin

Cuando un prueba una nueva herramienta de desarrollo siempre consigue algunos asuntos difíciles, yo programo en C/C++, y con PHP, y encontré a GO algo incomodo al principio, luego de probar las virtudes de GO estoy convencido de que es una de las mejores herramientas de desarrollo que he Usado. Simple, elegante, y muy versátil: pero por sobre todo: 1) Rápido en desarrollo 2) Mas Rápido para la ejecución 3) Resuelve el problema de la Memoria y la basura 4) Concurrente 5) Es genial

Collapse
 
ogfris profile image
Fris

I moved from PHP to Go as well many months ago, i'd say the best thing to do is to practice but not only in the web domain, you should also try to build programs using qml or sciter-sdk on the front-end for example!

Collapse
 
biros profile image
Boris Jamot ✊ /

You're right but I feel like I would be unable to build a frontend app right now. It would take a long time before I can feel comfortable with that. As I only work on production, I haven't that time.

Collapse
 
ogfris profile image
Fris

Bonne chance :)

Collapse
 
pim profile image
Pim

Thumbs up for still rocking PHP, in the banking world of all places! I've dipped my toes in the go pool recently as well. A lot of things to love, some to hate. Not sure whether it'll usurp .NET Core for me yet.

Collapse
 
biros profile image
Boris Jamot ✊ /

As an alternative to vim, it seems that Visual Studio Code is very good option for Go dev with the Go plugin. It's the only graphical editor that works pretty well out-of-the-box.

Collapse
 
dimensi0n profile image
Erwan ROUSSEL

You should try gin or echo. They are very good micro frameworks

Collapse
 
rhymes profile image
rhymes

This might help as well. They are slides (and examples) from a talk on how to structure Go code.

Collapse
 
ruslanuchan profile image
RuslanUchan

Way to Go! 😁 I'm also learning Go a few days back and find that it's an interesting language. Out of curiosity, do you have any project in mind after wrapping up learning the fundamentals?

Collapse
 
biros profile image
Boris Jamot ✊ /

Yes, I'm about to start a new project in the next days.
It's an API with a NoSQL db.
I'll be working with another newbie.