DEV Community

My Journey from PHP to Go

Boris Jamot ✊ / on September 20, 2018

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 commun...
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.