📜 ⬆️ ⬇️

Go language from the point of view of a PHP developer

Go is a language invented by Robert Griesemer, Rob Pike and Ken Thompson , which was announced in November 2009 with versions for Linux and Mac OS X. Now the developers are working at Google, but also long before that they were very famous and respected by humans (for example, UTF-8 encoding was invented by Thompson and Pike for use as the primary encoding in Plan 9).

Personally, I decided to see what kind of language after this video of Rob Pike's speech was published here . I do not study Go for a very long time, but even during the two weeks I spent studying it, I managed to do several projects on it, for example, a backend for the JsHttpRequest library ( JsHttpRequest ; my backend on Go ). I still use JsHttpRequest because of its convenience (from both JS and PHP). In the article I want to talk exclusively about my feelings after programming in PHP and JavaScript, with virtually no code examples.

In a nutshell about the language

The official site of the project is http://golang.org . There is a short tutorial, a description of the language and built-in packages (packages), as well as links to Wiki, benchmarks and a list of third-party packages .

Go is a statically typed language with a garbage collector that feels similar to both C and Javascript (many constructs also resemble other languages, such as Python, Pascal, and Java). Go is a compiled language, and compiled into native executable code (as opposed to Java and .NET) and in terms of performance it is quite close to C.
The language has a slightly weird syntax (for example, str definition of a string looks like str string , not string str , like in almost all other languages), native support for UTF-8, immutable strings (immutable strings) and support for dynamic typing using interfaces (not confused with interfaces in other OOP languages)

I will give a minimum of code examples - if you want to take a look at the code on Go, I recommend reading the tutorial .

Why is the language considered from the point of view of a PHP developer, and not <Java, .NET, Ruby, Python, ...> ?

I involuntarily compare with PHP because I started to program in PHP, I still program on it for about 7 years with a few interruptions and I wanted to see some language that would fundamentally be different from PHP, but at the same time suitable for the web. Of course, Google’s language cannot but satisfy the second criterion, so I decided to pay attention to it :).

It is not entirely correct to compare PHP and Go, since these are languages ​​for various purposes - they rather complement each other, but I still decided to compare Go and PHP in the area for which PHP is sharpened - for creating websites

Advantages compared to PHP

- Compiled language with static typing, which allows you to achieve excellent performance (10-100 times faster than PHP)
- Perfectly suited for writing system utilities and daemons
- Elimination of a huge number of runtime errors due to strong typing
- Exceptional support for Unicode and UTF-8: source texts and strings are mandatory in UTF-8, rich built-in mechanisms for working with UTF-8
- Excellent features of multi-threaded and parallel programming (concurrent and parallel)
- Tight integration with API * nix-systems, well-organized internal data structures for working with the system and files
- Good support for the latest web standards, such as WebSocket API

Disadvantages compared to PHP

- Insufficient integration with Apache (only work as a standalone web server or FastCGI from third-party developers)
- A language with not fully established syntax and capabilities - some projects on Go that are less than a year old are no longer compiled
- Lack of dynamic connection code (the inability to load and unload, say, * .so-files on the fly)
- Much less usable dynamic capabilities
- Few third-party libraries - except for MySQL, PostgreSQL and NoSQL-based databases you will not connect
- Lack of official support for Microsoft Windows ®
- It is advisable to have C programming experience in order to better understand the semantics of the language

Feelings in general

Go is the first language with static typing that I have seen, where it doesn’t really hinder programming and doesn’t force to write 2-3 times more code than in PHP or JavaScript. Also, Go is compiled into native code, and positioned as a language for writing system utilities and daemons. The language and the compiler do not cause the sensation of something monstrous and unwieldy, like Java or .NET (which, in fact, is the main reason why I do not write in Java).

On the other hand, one feels that the language is still developing rapidly (for example, I saw a package called html one day before my eyes, which appeared almost immediately after I downloaded and installed myself a Go-environment, so I had to build it again :)). Also, as a replacement for PHP, it doesn’t fit very well, because for each host you have to write a whole web server and ensure that it doesn’t accidentally fall down :), and you also need to bother with launching these daemons when the server is restarted, for example . I am silent that the maximum on virtual hosting that you can do on Go is to connect your CGI handlers to Go, which will not work much faster than PHP, if it works faster at all.

In general, the language itself and the built-in libraries are stable enough to allow Go to be used in production (for example, golang.org itself is running the server on Go, namely, it's just godoc running with the -http = "key. .: 80 ") But, since I don’t have as much experience in network and system programming as I would like, I quite often come across errors in using libraries, which could have been avoided if I had experience in network programming and writing demons. But, in general, it seems to me, if you wanted to learn Java or ASP.NET, try Go better - this language is unlikely to leave you indifferent.

Source: https://habr.com/ru/post/102523/

All Articles