An introduction to Go programming language

go

Last week at DailyDrip.com, we talked about the Go programming language.

We saw that Go is a general-purpose programming language with a very concise and small syntax. We learned Go was created by Google to solve their real-world problems.


Why use Go?

Go has the excellent tooling and a great standard library that helps you build robust programs. The standard Go library is very complete. Almost everything has a built-in package. HTTP servers to clients, cryptography, managing file systems, testing, and more.

To keep your code clean and performant, Go gives you tools for formatting code, importing package dependencies, linters, race detectors, profiling, and others.

And it's most appealing feature is the concurrency model. The language was built from the ground up to provide a compelling yet straightforward concurrency model that enables one to run millions of concurrent processes on a single machine.

Basics

In the second episode, we learned the basics: how packages work, how to import and download packages so you can reuse code.

As a typed language, we showed what are the basic types available and controls structs like: if/else, for, range and switch. And how variables, functions and methods works, because despite not being an object-oriented language we can have an object-oriented style of programming in Go.

You can check more of the content here in our website.

Concurrency

In the third episode, we could see how powerful Go's concurrency model is using goroutines and channels.

The most basic definition of goroutine is a lightweight thread managed by the Go runtime. They behave like OS threads, but they are far cheaper regarding memory consumption, setup, teardown and switching time.

To provide communication between the goroutines, Go provides channels that connect two concurrent goroutines and allow the transmission of data between them without you having to worry about shared state.

Developing a real Go programming

In the fourth episode, we saw how to develop a real Go CLI. It was a gif downloader, which searches and downloads gifs concurrently.

Developing this cli, we were able to see how to use environment variables, parse cli flags, make HTTP requests, parse JSON, write files to disk. Also, we will use goroutines to make all that concurrent and fast. All that using just the standard library.

Testing

And last but not least, we learned about unit testing in Go using the built-in testing package. We created a basic calc package with a simple Sum function and created a simple test to it. After that, we did some table driven test, which consists of a table, where each entry is a test case with inputs and expected results. This kind of test method is very common in Go.

Another cool feature is Parallel testing, in Go we can easily say that a test should run in parallel, giving you the possibility of dramatically decreasing the time to run your test suite.

Go tooling even provides out of the box code coverage.

List of episodes