Read
July 1, 2020

Introduction to Golang

Morvana Bonin
Backend Software Engineer

* article developed in partnership with Andreia Camila da Silva

Golang or simply Go is an open source programming language created at Google in 2007 by Robert Grieserer (V8), Rob Pike and Ken Thomps (UNIX & UTF-8), but its announcement was only made in 2009 and its version 1.0 came out in 2012.

Although the language is already over 10 years old, only recently have we heard more about it, especially with its use being adopted by commonly and widely used software such as Docker and Kubernetes, developed in the language.

The Go language structure is very similar to the C language, but its learning curve is simpler. Go is a language created to make the most of computers with multi-core resources, thus facilitating the compilation of code in an efficient and naturally cooperative way with the abstractions of current operating systems.

Golang Installation

A quick note before dealing with the installation part: it is important to know that it is not necessary to have Go installed in your environment to take the first steps – it is possible to just "play" and take the first steps with the language doing a Hello World! by two different ways and learn more about the syntax of the language. They are: [a tour by Go] (https://go-tour-br.appspot.com and also [Playground Go] (https://play.golang.org).

Go's installation is as simple as the ease of learning the language and it is possible to do it on Linux OS, MacOS and Windows.

The stable version used for the installation and writing of this article is go1.14.4, it is possible that at the time of reading this article, the version is more up to date, so it is advisable to see the official language site in the downloads section https://golang.org/dl/

Linux

To complete the installation, you need to download the Go binary. You can use the curl or wget commands.

/code
curl -O https://storage.googleapis.com/golang/go1.14.4.linux-amd64.tar.gz
/code 

Check the .tar using sha256sum

/code
sha256sum go1.14.4.linux-amd64.tar.gz
/code 

By using the following command, extract inside the /usr/local directory to install the Go distribution

/code
$ sudo tar -C /usr/local -xzf go1.14.4.linux-amd64.tar.gz
/code 

You need to export the go and bin file, open ~/.profile in an editor

/code
$ sudo vim ~/.profile
/code

Paste or write these two lines at the end of the file

/code
export PATH=$PATH:/usr/local/go/bin
/code 

Save the file and reload it with the following command

/code
source ~/.profile
/code

To test the installation, simply run the command

/code
$ go version
/code

macOS

Download the stable and recent version of the Go binary package for MacOS. As with Linux, use the curl command

/code
curl -o golang.pkg https://dl.google.com/go/go1.14.4.darwin-amd64.pkg
/code

To install it, just double-click the downloaded file to start the installation wizard or use the following command line

/code
sudo open golang.pkg
/code

When installing the package, it should already create and put the /usr/local/go/bin directory in its PATH environment variable. It may only be necessary to restart open terminal sessions for the change to take effect.Otherwise, it is necessary to follow similar steps to the one previously given in the Linux installation sessionOpen the file ~/.profile in an editor

/code
$ vi ~/.profile
/code

Paste or write these lines in the file

/code
export GOROOT=/usr/local/go
export PATH=$GOROOT/bin:$PATH
/code 

GOROOT is the location where the Go package is installed on your systemAt the end, the PATH export sets the variable to access the entire binary system.To test the installation, simply run the following command

/code
$ go version
/code

Windows

For Windows users, two installation options are provided (in addition to installation from source): a zip file that requires you to set some environment variables, and an MSI installer that sets up your installation automatically.

For the .zip file, download the file at https://golang.org/dl/, then extract the content to the directory of your choice (the suggestion given by the documentation is c:\Go)

Remember that you need to add the bin subdirectory of your Go root (e.g. c:\Go\bin) to your PATH environment variable.

Now using the MSI file, open it and follow the instructions to install the tools. By default, the installer puts the Go distribution in c:\Go.

Unlike the installation by using the zip file, the installer should put the directory c:\Go\bin in its PATH environment variable. It may only be necessary to restart any open command prompt for the change to take effect.

GVM

Another alternative to install and manage Go versions is [gvm](https://github.com/moovweb/gvm)Just run the command below to install the gvm-installerbash <<(curl -s -S -L https://raw.githubusercontent.com/moovweb/gvm/master/binscripts/gvm-installer)Then install the desired version of Go, using

/code
gvm install go1.14
gvm use go1.14
/code

It's worth knowing that some versions of Go have some slightly different configurations, for example, the older versions use GOPATH to set the working directory. From go1.11 onwards, this is no longer so common and, for go1.14, it is already considered deprecated.

Hello World!

Testing the installation

Let's go to the traditional Hello World. With Go language, what has been transcribed here comes mostly from the documentation of the language itself.

Create an aprendendo_go directory and create a file called hello.go inside it. In the file, write these code lines and save it

/code
package main
import "fmt"
func main() {fmt.Printf("hello world\n")
}
/code

Then build it by running the following command in a terminal inside the aprendendo_go directory

/code
$ go build hello.go
/code

The above command will create an executable called hello in the aprendendo_go directory next to its source code. Run it by running the following command to see the greeting:

$ ./hello
hello world

It is worth mentioning that there are already several plugins (some of them maintained by the Go community itself) of editors and IDEs to make development more productive. Here is the link for some of them:

This was the first post about Go, which will be a part of a sequence of Posts that Andreia Camila da Silva and I will do giving an introduction about the language.MacOS and Windows environments have not been previously tested. If there is any inconsistency, you can contact the authors for adjustments or even for knowledge exchange. You’re always welcome.

References and useful links

Language website https://golang.org

Applications developed in Go https://awesome-go.com

Practical introduction to Go using annotated example programs https://gobyexample.com

Russ Cox joined the team and made great contributions, his blog is very rich in content https://research.swtch.com

Learn Go from Zero https://www.youtube.com/watch?v=_MkQLDMak-4

Golang in 20 minutes by Wesley WilliansThe Go Programming Language por Alan A. A. Donovan and Brian W. Kernighan

https://www.linode.com/docs/development/go/install-go-on-ubuntu

https://tecadmin.net/install-go-on-macos

https://sourabhbajaj.com/mac-setup/Go/README.html

Thanks for reading!