π What is Go?
When I first read about Go and what it offers I decided to start and learn it. After some time I realized it has some amazing potential, and I wanted to share it here with you. Note that this is just a short blog post to show and explain what Go is and how it works, itβs not supposed to be a Wikipedia article π
π About Go
Go is a statically typed and compiled programming language. Go is designed at Google by Robert Griesemer, Rob Pike, and Ken Thompson. Syntactically Go is very similar to C, but it has memory safety, garbage collection, structural typing and many other advantages. Itβs an easy language for developers to learn quickly.
π€ Why has Go been created?
Go was intended as a language for writing server programs that would be easy to maintain over time. Itβs now being used for writing light-weight microservices and is being used for generating APIs that will later on interact with the front-end. So in short words I could say that it was mainly created for APIs, web servers, frameworks for web applications, etc.
ποΈ Why is Go βso goodβ?
Its easy concurrency is really easy to make and include in your projects. As you may know, network applications are really dependent of concurrency, and thatβs why Goβs networking features makes it even easier and better.
When declaring interfaces in Go, you donβt need to add like in other language the keyword implements
or anything similar. Which means we can create an interface and simply make another object implement it, we simply need to make sure that all the methods from the interface are in the object implementing it, just like any other language.
When looking at Goβs standard library we can see that we donβt always specifically need to get a third-party library. It goes from parsing flags when executing your program to testing.
Since Go get compiled into machine code, we need a compiler. For Go, the compilation time is quite fast compared to some other languages, one reason for that is that Go does not allow unused imports. If you want a comparison to another language, Go is significantly faster than C++ at compilation time, however it also results in Goβs binary to be bigger in terms of size.
And as always, who does not like programming languages that are cross-platform? Well, Go got you covered for that, from Android to Linux to Windows, just run go tool dist list
in your terminal to see it.
π€― How hard is Go?
This is a question you often get asked when you learn a new programming language and other people you know may want to learn it. For Go, itβs a simple answer because the language itself is simple. Goβs syntax is quite small compared to many other languages and therefore easier to remember. Most of the things can be remembered quite easily and this means you wonβt need to spend a lot of time at looking things up. You can start taking a look at Go here.
π A βHello world!β in Go
A hello world is often included when you explain what a language is, so here it is:
1 | func main() { |
π Did I hear web server?
Correct! Go is widely used for web servers and/or APIs. This can go from a very basic REST API to using Websockets. In comparison to other languages, Go does not need some overcomplicated web framework, Goβs standard library already has this implemented and ready for us. Of course there are third party libraries that implements some additions.
To create a very basic web server we need to import two default libraries like the following:
1 | import ( |
Now we can create a simple handler that will listen to the port 1337
and register a new /hello
route.
1 | func helloWorld(response http.ResponseWriter, request *http.Request) { |
Now run the code with go run main.go
and go to 127.0.0.1:1337/hello.
In around 3-7 lines of code we managed to start a web server and create a route that will display some text.
Now itβs up to you to make your creative projects :)
π₯ What is Goβs concurrency?
Goroutines
Python developers may know what coroutines are, however to include them in your project you need an additional library called asyncio. Yes thatβs not a big deal as most of the Python features relies on libraries anyways.
The difference with Go, is that you can easily create a so called Goroutine to make functions run concurrently. Hereβs a very easy example on how to create a Goroutine:
1 | func echo(s string) { |
As you may notice, I just had to add the keyword go
before calling the function to turn the function into a Goroutine. And of course I can call the echo()
function without having to mark it as a Goroutine. After running the code, here is a sample output I got:
1 | Hello |
You can clearly see, that the echo("Hello")
method runs without having to wait for the Goroutine to end, this is concurrency.
Channels
What makes Goβs concurrency different and unique from different languages are so called channels. You can see channels as being pipes that transfer data. This is used to send values and data from one goroutine to another one, or just to get data back from a goroutine.
Take this example:
1 | func sum(list []int, channel chan int) { |
This small example simply sums the numbers from a slice and puts the work between two goroutines so that they are being calculated concurrently and is therefore faster. Once both goroutines gave their output, it will calculate the final result and print it in the console. The arrows such as <-
simply describe from where to where the data goes, so either from the channel to the variable or from the variable in the channel.
π³ Does Go have objects?
Of course! Thereβs just one slight difference here. Go doesnβt directly have a type object
, but they have a type that matches the definition of a data structure that integrates both code and behavior. Itβs called a struct
. Let me show you a very simple example.
Structs
1 | type Rectangle struct { |
Implicit inheritance
1 | type Shape interface { |
As you can see from the example above, the struct Rectangle
implements the interface Shape
but nowhere in the code itβs clearly written, that it implements it.
π Popular open-source projects
As you might have expected, there are some really popular projects that are using Go. Here is just a small list among a lot of other projects:
π« How do I get started?
Iβm glad youβre interested in learning more about Go by yourself!
Installation
The installation is really easy regardless of the operating system you have, simply go to the download page and follow the instructions. Oh and remember, Go works on every operating system!
Learning resources
I donβt really have specific learning resources but these are the things Iβve used to start to learn and code in Go:
- A tour of Go
- Codecademy
- Effective Go
- Goβs Documentation
- Go by Example
- Sololearn (My favorite as you get a better overview of concurrency)
Iβd be happy to see you become part of the Gophers!