πŸ“— What is Go?

@
11 min. read | 1337 views

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
2
3
func main() {
fmt.Println("Hello world!")
}

🌐 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
2
3
4
import (
"fmt" // This is to give output when requesting a page (also used in the hello world example)
"net/http" // This is needed to handle requests, etc.
)

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

1
2
3
4
5
6
7
8
func helloWorld(response http.ResponseWriter, request *http.Request) {
fmt.Fprintf(response, "Hello from the Gopher side!")
}

func main() {
http.HandleFunc("/hello", helloWorld)
http.ListenAndServe(":1337", nil)
}

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

1
2
3
4
5
6
7
8
9
10
11
func echo(s string) {
for i := 0; i < 5; i++ {
time.Sleep(100 * time.Millisecond)
fmt.Println(s)
}
}

func main() {
go echo("world")
echo("Hello")
}

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
2
3
4
5
6
7
8
9
10
Hello
world
world
Hello
world
Hello
Hello
world
world
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
func sum(list []int, channel chan int) {
sum := 0
for _, value := range list {
sum += value
}
channel <- sum // Here we send the sum to the channel
}

func main() {
list1 := []int{1, 2, 3}
list2 := []int{9, 8, 7}

channel := make(chan int) // Here we create a channel that accepts integers
go sum(list1, channel)
go sum(list2, channel)
x := <-channel // Here we receive data from the channel
y := <-channel

fmt.Println(x, y, x+y) // Output: 24 6 30
}

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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
type Rectangle struct {
Width int
Height int
}

// The '(rect *Rectangle)' before the method name shows to which object the method will operate, in this case the 'rectangle' object.
func (rect *Rectangle) Area() int {
return rect.Width * rect.Height
}

func main() {
rect := Rectangle{Width: 10, Height: 5}
// As expected, the output is 50.
fmt.Println("Area: ", rect.Area())
}

Implicit inheritance

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
type Shape interface {
Area() float64
}

type Rectangle struct {
Width float64
Height float64
}

func (rect Rectangle) Area() float64 {
return rect.Width * rect.Height
}

func main() {
var s Shape = Rectangle{Width: 10, Height: 50}
fmt.Println(s.Area())
}

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 :DBe 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!