I’ve spent time with Go having fun these past few months. I offer an introduction to the preferred language of Google. I liked it a lot for a lot of reasons and today I’m talking about it for 5 minutes.
Hey! Ho! Let’s Go!
Yes. All the headings are a play on words with Go in this article. It’s not funny, but it gives me pleasure.
In 2007 none of the existing languages were good enough for the problems that Google was facing. Three of the employees, Ken Thompson, Rob Pike and Robert Griesemer, reserved a small quiet room in Google premises to discuss it. And to fix Google’s problems, they just decided to create a new language. And with their monster experience in the business, you can imagine that they did their own thing. Because yes, these guys are not interns. They helped create the C, Unix or even UTF8 language. We are on hairy dev experts.
They wanted three things: extremely fast compilation, efficient execution, and very easy-to-use language.
For them, C ++ is formidable on the execution side but complex to use and with too long compilation time. Java is a better-rated compilation, but still too complex to use. Python is easy to use, but not fast enough. All languages have a big flaw for our three legends. And it was with the idea of solving the three problems at the same time that Go was imagined.
Du Go à go-go
In 2009 the language was publicly announced and in 2012 a first release landed on the Internet. And the promise is kept.
Go is a compiled and strongly typed programming language. In terms of syntax, it’s pretty much the same face as C. Compilation allows us to tend towards the best possible performance, but wastes developers’ time. Not with Go. The compilation is almost instantaneous. So we have the discipline and speed of a compiled language with the speed of iteration of an interpreted language.
After compilation Go will generate a single file. This file contains all of your code and can be used as-is. It makes deployments much simpler. Also Go is a multi-paradigm language. You can do procedural and/or object-oriented. The object-oriented is weird and very limited, we will come back to this.
Go also comes with a garbage collector which once again takes the complexity out of the developer. Go also makes competitor management easier with its Go-routines.
Finally, Go to limited features and suddenly a way of doing things limited. It may seem like a drawback, but it is wanted by the creators. Everyone works in the same way with Go since the possibilities are reduced. The goal is that no one is lost when they come across a code they don’t know. Rob Pike explains this point better than I do in this video just above.
Code from behind the fagots
For your very first introduction, I’m going to show you some code found in the official go-round. I’m going to modify it a lot to show you several concepts at the same time and go faster.
This is Hello World. Each program is made up of packages and execution begins in the main package. Other Go packages can be imported using “import”. Here we import fmt which will allow us to print hello world in the main execution function main (). Pretty simple isn’t it?
Now we add a function and as you can see it is strongly typed. The x and y parameters of the function add are typed and the return of the function int must also be typed.
Then in the main function, we create two variables that we initialize directly. We then display the result of the first function via fmt. Note that if I hadn’t used the variables i and j the Go compiler would have insulted me immediately.
As I said before, the object-oriented is weird with Go. There is no class, but methods on types. Interfaces are implicitly implemented. There is no keyword implement, it is frankly confusing, let’s take a closer look.
Finally, let’s take a look at what a Go-routine looks like.
Even if the concept exists in other languages, Go-routines are one of the great features of Go. These are functions that can be launched in competition with other functions. It looks like threads but they are extremely less greedy! Go-routines communicate with each other using channels. If you want to see more code to get a better opinion this site is just perfect.
Used in all sauces
People often use the name Golang to refer to Go. The name comes from the official website golang.com. The go.com domain is already taken by a Disney site. Google had no choice and now the name Golang is everywhere. Note that Google supports Golang but that the language remains open-source.
In any case, it didn’t stop the whole world from using Golang. Obviously, you have Google that uses it everywhere. Docker is completely written in Golang. It’s used in production at Amazon, IBM, Netflix, Facebook, and even DigitalOcean. And it’s been over 10 years now, so the ecosystem has had time to develop.
So I go?
the adaptation by developers Go continues its slow rise for 10 years reaching the top 10 popularity on GitHub. However, it is not used that much in business. He only reached 13th position in the last StackOverFlow poll. That said, it is in the top 3 languages most wanted by developers.
Personally, I love Go. I have not encountered any of the language problems that are in fact present. I continue to play with it, but I don’t see myself completely passing the milestone of writing nothing in NodeJS at home. I love NodeJS too much to do that. By cons, my plan in the future is to make WebAssembly modules in Go and call them via import when it is merged stable everywhere! Too much impatience!
Should you let go of everything and go Go because it’s the future? No. No language is the future. The future will always be made up of many languages. But if you want to test a super simple and high-performance tool for your backend: it’s really interesting. I strongly advise you to take at least the official interactive tour to make your own opinion.