All you need to get started

Image for post

It’s time to learn Golang. [Image source]

This post intends to be an introduction to the Go programming language, also known as Golang.

Disclaimer

I’m not an expert in Go. In fact, I’ve started learning about Go very recently. Therefore, take everything in this post with a pinch of salt.

Then… why am I writing a post about Go? It’s simple: I want to use this post as a tool to reinforce my learning process. I believe that working on a blog post and publishing it will force me to get every detail straight. Moreover, I’ll be crafting the guide that I would have liked to have found in the first place (and, therefore, it is likely that it will become the guide that you were looking for as well). This also means that this post will probably be evolving as I learn new aspects of Go.

Keep in mind, though, that despite of the fact that I’ll be working on every detail of the post, I may make mistakes. I encourage you to indicate in the comments section any errors that you may find, and I’ll gladly correct them.

If this disclaimer has let you down and you are not interested in reading this post anymore, here are some fantastic resources to get started with Go:

If you, however, decide to stay and keep reading… welcome!

A bit of history

Golang was born in Google, and it was created by Robert Griesemer, Rob Pike and Ken Thompson. It was discussed for the first time on September 21, 2007 and eventually became an open-source project on November 10, 2009.

As reported in Go’s site, this language was born due to the commonly necessary choice between an efficient compilation, efficient execution or ease of programming at the time of choosing a programming language to work with. Therefore, the main goal of Go was to create a language that would achieve the ease of an interpreted and dynamically typed language, while preserving the efficiency and safety of a statically typed, compiled language. Another one of the main goals was to take advantage of the growth of multicore CPUs, making concurrency one of the priorities of the language.

To get a deeper insight into the goals of Go, I’d recommend reading this article, written by Rob Pike. Some of the goals mentioned in this article include finding a solution for:

  • slow builds
  • uncontrolled dependencies
  • each programmer using a different subset of the language
  • poor program understanding (code hard to read, poorly documented, and so on)
  • duplication of effort
  • cost of updates
  • version skew
  • difficulty of writing automatic tools
  • cross-language builds

Considering that Google works with large-scale systems that have huge codebases, Go was primarily developed as a language “in the service of software engineering”, attempting to solve the most common issues in large systems.

Getting started with Go

Setting up our development environment

In order to follow through this post, you have two options:

  1. Set up a local development environment
  2. Use Go’s official online playground

I will not go into the installation details for each platform, but here are some guides:

If you, however, prefer not to commit for now and test out the language, option 2 is a perfectly valid option. In any case, I recommend you to go through this introduction executing the examples, instead of just reading the code: I’m sure you would get a better grasp of the language.

Hello world!

Well, first things first! Let’s get started writing our first program in Go. In the following sections I’ll be explaining in more detail each part of our program. We may print our “Hello world!” message with the following few lines of code:

package main

import "fmt"
func main() {
  fmt.Println("Hello world!")
}

We’ll save the above snippet in a file called Main.go and then run the following instruction in our CLI:

go run Main.go

Voilà! If everything worked correctly, you should see our message printed in the terminal. Congrats! You’ve written your first Go program!

Let’s make an initial explanation of the above snippet, so that we start getting our first building blocks. Go programs are organized into packages, and one special package is “main”, which must be used in executable commands. We may now move towards the import statement, which allows us to include packages in our program. It may be worth mentioning that we may import multiple packages with the following syntax:

import (
  "<package_1>"
  "<package_2>"
)

In our example, we can see the “fmt” package, which is commonly used to read from stdin or write to stdout. We’ll be discovering this package in more detail as this article develops.

Finally, we may find the “func main()” declaration which, unsurprisingly, declares the main function of our Go program.

#software-engineering #go #golang #programming #software-development

An Introduction to Golang
1.15 GEEK