From the translator: translated for you by Ilad Leev’s article on switching from Python to Go . The article will be useful not only for novice programmers, but also for everyone who is somehow interested in Go.I like Python. This language has been my favorite for the past five years. He is friendly, efficient, and easy to learn. Used for almost everything: from creating simple scripts and web development to data visualization and machine learning
')
The gradual “ripening” of Go, the extensive community, and the fact that more and more companies are adopting this language after successful tests have made me pay attention to it and delve into the literature.
But this post is not about what is better - Python or Go : there are a lot of comparisons on the net. In my opinion, it all depends on the application. I'm going to talk about why I chose Go, giving a few tips and links to useful resources for anyone interested in the topic.
Skillbox recommends: Practical course Python-developer from scratch .
We remind: for all readers of "Habr" - a discount of 10,000 rubles when recording for any Skillbox course on the promotional code "Habr".

Observations
The first thing I did at the beginning of my journey was learning the excellent official tutorial “
Tour Of Go ”. It gives an understanding of the syntax of the language.
In order to improve my knowledge, I also read the book “
Go for Python Programmers ”, which allowed me to proceed to the next stage - trial and error.
I took the usual functions that I used in Python (JSON serialization or working with HTTP calls), and tried to write them on Go. Thanks to this visual comparison, I was able to identify the key differences between languages.
Project LayoutFirst of all, Python does not require a specific directory hierarchy, whereas Go does.
Go uses a “standard” layout, which is a bit more complicated and requires more work. On the other hand, as a result, we get a well-structured code base, where the modular structure is used, and when the project is extended, the code remains strictly ordered.
The official tutorial “
How to Write Go Code ” explains how to organize your work.
Static strong typingGo is statically typed, and it will make those who are used to dynamically typed languages like Python and Ruby feel uncomfortable.
There is no doubt that dynamic languages are more susceptible to errors; from the developer’s side, more effort is required when validating input data. An example is a function that calculates the sum of two integers. Passing a string to a function (which happens not so rarely) will result in a TypeError error.
In Go, this cannot happen, because here you need to declare a type for each variable and function and what type of variable the function will return.
At first it was annoying: it seemed to me that this Go feature slows down the work, but then it came to be understood that, in fact, the announcement of everything saves time, and the probability of an error decreases.
Native concurrencyGo has native support for concurrency using subroutines and channels - this is convenient.
The concept of channels initially seems somewhat confusing. However, over time, it becomes more understandable, and you begin to enjoy new opportunities, actively working with them.
Here is a visualization of everything said from
Ivan Danilyuk .
package main func main() {

More examples
here and
here .
Work with JSONWell, json.loads () is no longer. In Python, everything is simple: use json.loads, and there are no problems.
But in Go, a statically typed language, this operation becomes more complicated.
Here when using JSON everything is predefined. Any field that does not fit into a given structure will be ignored, and that is good. This can be thought of as a pre-agreed protocol between the two parties. The data you received in JSON must be expected, and the fields and JSON types are “matched” by both parties.
{ “first”: “Elad”, “last”: “Leev”, “location”:”IL”, “id”: “93” }
type AccountData struct { First string `json:"first"` Last string `json:"last"` Location string `json:"location"` ID string `json:"id"` }
Of course, you can deserialize JSON without structures, but if possible this should be avoided and the static nature of the language should be taken into account.
JSON decoding on GO is best
explained in this post or
here .
Lazy to convert your JSON to Go-structure? No problem,
this tool will do everything for you .
Clean codeThe Go compiler will always try to keep your code clean. It considers unused variables as a compilation error. Go uses a unique approach that allows the system to solve most formatting problems. So, Go will launch the gofmt program while saving or compiling and will automatically correct the formatting.
You do not care about variables? Okay! Just use _ (underscore) and assign it to an empty identifier.
The mind-tutorial for this part of working with a language is the information from “
Effective Go ”.
Finding the right library and frameworkI used Python frameworks and libraries like Flask, Jinja2, Requests, and even Kazoo, so I was afraid I wouldn't find anything suitable for Go.
But the community has already solved these problems: the language has its own unique libraries that allow you to completely forget about what you used before.
Here are my favorites.
Python Requests => net / httpnet / http provides a convenient and easy-to-use HTTP client and server implementation.
Flask + Jinja2 => GinGin is an HTTP web framework with a very simple API: parameters in the path, downloadable files, group routing (/ api / v1, / api / v2), custom log formats, serving static files, HTML rendering and really powerful custom middleware.
Rate
this benchmark.
CLI Creation => CobraCobra is a library for creating powerful CLI applications, as well as a program for generating applications and batch files.
Cobra is used in many large Go-projects, including Kubernetes, etcd and OpenShift.
Here are some more libraries that I highly recommend:
Viper ,
Gonfig and this awesome list -
Awesome-Go .
Other helpful resources
[1]
Francesc Campoy - you definitely need to rate this
YouTube channel and
GitHub profile .
[2]
GopherCon - video .
[3]
Go Web Examples .
[4]
Golang Weekly ,
Gopher Academy ,
Golang News - Twitter accounts.
Summing up
Being a regular Python user for five years, I was afraid that the transition to Go would be painful.
But no: there are the achievements of the Go community, which expands and complements the possibilities of the language, as well as various useful resources that will help you with the transition.
Go is developing rapidly, and I hope that Google will be able to make it the main language for writing cloud applications and infrastructure.
Join now!

Skillbox recommends: