Understand Go in 5 minutes
I’ve been spending time with Go for fun these past few months. I’ll give you an introduction to Google’s favorite language. I liked it a lot for many reasons and today I’ll tell you about it for 5 minutes.
Hey! Ho! Let’s Go!
Yes. All the headings are a pun 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 Google was experiencing. Three of their employees, Ken Thompson, Rob Pike and Robert Griesemer, reserved a small and quiet room in Google’s offices to discuss the issue. And to solve Google’s problems, they just decided to create a new language.
With their monster experience in IT, you can imagine that they did something clean. Because yes, these guys are not interns. They helped create the C language, Unix and UTF8. They know what they are doing.
They wanted three main things for this new language :
- extremely fast compilation
- efficient execution
- very easy to use language.
For them, C++ is awesome on the runtime side, but complex to use and with too much compilation time. Java does better on the compilation side, 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. Go was imagined to solve all three problems at the same time.
Gotta Go fast
In 2009 the language is publicly announced and in 2012 a first release lands on the Internet. And the promise is kept.
Go is a compiled and strongly typed programming language. Syntactically, it looks almost like C. Compilation allows for the best possible performance, but wastes developers’ time. Not with Go. Compilation is almost instantaneous. So you have the discipline and speed of a compiled language with the iteration speed of an interpreted language.
After compilation Go will generate a single file. This file contains all your code and can be used as is. It makes deployments much simpler. Also Go is a multiparadigm language. You can do procedural and/or object oriented. Object oriented is weird and very limited, we’ll come back to that.
Go also comes with a garbage collector which again takes away complexity for the developer. Go also makes it easier to manage concurrency with its Go-routines.
Finally Go has limited features and a limited way of doing things. It may seem like a drawback, but it’s what the creators wanted. Everybody works 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 me in this video just above.
Good to Go
For your very first introduction, I’ll show you some code from the official go website. I will modify it a lot to show you several concepts at the same time and go faster.
package main import "fmt" func main() { fmt.Println("hello world") }
Hello World. Each program is composed of package and execution starts 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. Pretty simple right ?
package main import "fmt" func add(x int, y int) int { return x + y } func main() { var i, j int = 10, 2 fmt.Println(add(i, j)) }
Now, we add a function and as you can see it is strongly typed. The x and y parameters of the add function are int typed and the return of the function must also be typed.
Then, in the main function, two variables are created and initialized directly. The result of the first function is then displayed via fmt. Note that if I had not used variables i and j the Go compiler would have insulted me immediately.
Like I said before, object-oriented is weird with Go. There’s no class, but methods on type. Interfaces are implicitly implemented. There is no implement keyword, it’s really confusing, let’s have a closer look.
package main import "fmt" type Developer interface { Code() string } type GoDeveloper struct { } func (g GoDeveloper ) Code() string { return "Go code" } func main() { var goDeveloper Developer = &GoDeveloper{} fmt.Println(goDeveloper.Code()) }
Anyway, let’s take a look at what a Go-routine looks like.
package main import ( "fmt" "time" ) func say(s string) { for i := 0; i < 5; i++ { time.Sleep(100 * time.Millisecond) fmt.Println(s) } } func main() { // go-routine is here with go keyword go say("world") say("hello") }
Even if the concept exists in other languages, Go-routines are one of the key features of Go. They are functions that can be launched in concurrency with other functions. They are similar to threads but are extremely cheap to use ! Go-routines communicate with each other using channels. If you want to see more code to get a better feel for it this site is just perfect.
Go Everywhere
People often use the name Golang to talk about Go. This name simply comes from the official website golang.com. The domain go.com is already taken by a Disney site. Google had no choice and now the name Golang is everywhere. Note that Google supports Golang but the language remains open source.
At least that didn’t stop the whole world from using Golang. Of course, you have Google using 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 around for more than 10 years now, so the ecosystem had time to develop.
Should i Go ?
On the adaption side, Go developers continue their slow rise over the last 10 years, reaching the top 10 of popularity on GitHub. However, it is not used that much in companies. It only reaches the 12th position in the latest StackOverFlow survey. That said, it is in the top 3 most wanted languages by developers.
Personally I love Go. I haven’t encountered any of the language’s problems that are present. I continue to have fun with it, but I don’t see myself completely switching from NodeJS at home.
I love NodeJS too much to do that. However, my plan for the future is to make WebAssembly modules in Go and call them via import when they are merged stable everywhere!
Should you drop everything and use Go because it’s the future? No. No language is the future. The future will always be composed of many languages. But if you want to test a very simple and powerful tool for your backend : it’s really interesting. I strongly advise you to do at least the official interactive tour to make your own opinion.
Epilogue
They are currently incrementing Golang version 1 (now in 1.14) with new features. In the official blog they talk about upgrading to version 2 around version 1.20. I can’t wait to see what will become of the language and its adoption by then.