Learning Go by examples: part 1 - Introduction & Installation
Tags: go, beginners, tutorial
Published: 2021-07-15 06:17:11

---
title: Learning Go by examples: part 1 - Introduction & Installation
published: true
description: Serie of article in order to learn Golang language by concrete applications as example
serie: Learning Go by concrete examples
tags: Golang, beginners, tutorial
series: Learning Go by examples
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/utysh7y325t3ly8rzbxk.png
---

## Why Golang?

![love gopher](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y4eq8zzjvrwbyj14x0lr.png)

Former Java developer for 10 years, I discovered [Golang](https://golang.org/) (aka Go) over 6 years ago and fell in love with its simplicity and learning curve. It's easy to start creating an application in Go but you have to dig deeper to avoid falling into certain pitfalls ^^.

I like the explanation by example, so in this new series of articles, I will try to introduce you to Go with concrete applications in each article.

Let's start this serie with a prerequisite ;-).

## Installation

The first thing to do is to install Golang in your local computer. You can follow the [installation procedure on the official website](https://golang.org/doc/install) but I recommend to install and use [G](https://github.com/stefanmaric/g), a simple Go version manager, that will allow you to install and update the versions of Go by specifying which version you want.

For bash:
```
curl -sSL https://git.io/g-install | sh -s -- bash
```

For zsh:
```
curl -sSL https://git.io/g-install | sh -s -- zsh
```

That will download the `g` script, put it inside `$GOPATH/bin/`, give it execution rights with `chmod`, and configure your default shell's initialization file, setting the `GOPATH` & `GOROOT` environment variables and adding `$GOPATH/bin` to the `PATH`.

Then you will prompted to install the latest version of go; you can skip this step and install your preferred version with `g` later.

**NOTE**: You must restart your current shell session for it to read these new env vars in order to use `g` or `go`.

In my side, I have already an alias `g=git`, so I added another alias in my `.zshrc` file:

```bash
alias ggovm="$GOPATH/bin/g"; # g-install: do NOT edit, see https://github.com/stefanmaric/g
```

Usage:
```
$ g -h

  Usage: g [COMMAND] [options] [args]

  Commands:

    g                         Open interactive UI with downloaded versions
    g install latest          Download and set the latest go release
    g install <version>       Download and set go <version>
    g download <version>      Download go <version>
    g set <version>           Switch to go <version>
    g run <version>           Run a given version of go
    g which <version>         Output bin path for <version>
    g remove <version ...>    Remove the given version(s)
    g prune                   Remove all versions except the current version
    g list                    Output downloaded go versions
    g list-all                Output all available, remote go versions
    g self-upgrade            Upgrades g to the latest version
    g help                    Display help information, same as g --help

  Options:

    -h, --help                Display help information and exit
    -v, --version             Output current version of g and exit
    -q, --quiet               Suppress almost all output
    -c, --no-color            Force disabled color output
    -y, --non-interactive     Prevent prompts
    -o, --os                  Override operating system
    -a, --arch                Override system architecture
    -u, --unstable            Include unstable versions in list
```

The `g` command that will interest us especially is the command `g install`, which we can use like this:

```
$ g install [version]
```

You can also install directly the latest version of Go:

```
$ g install latest
```

Go installation:
```
$ g install 1.18.4
```

Now we can check our current Go version:
```
$ go version
go version go1.18.4 darwin/arm64
```

And if, later, you want to switch to another version of Go you have previously installed, it's easy with the `g` command:

```
$ g

    1.13
  > 1.18.4
```

## Conclusion

Cool!
We now know how to install and switch between different versions of Go. We now can create our first applications!