# 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 😉

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:

## 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:

Now we can create a simple handler that will listen to the port 1337 and register a new /hello route.

Now run the code with go run main.go and go to 127.0.0.1:1337/hello.

As you can see the web server works like a charm!

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:

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:

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:

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.

### Implicit inheritance

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.

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?

Be part of the Gophers :D

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:

I’d be happy to see you become part of the Gophers!