Learning Go by examples: part 1 - Introduction & Installation

Aurélie Vache - Jul 15 '21 - - Dev Community

Why Golang?

love gopher

Former Java developer for 10 years, I discovered Golang (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 but I recommend to install and use 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
Enter fullscreen mode Exit fullscreen mode

For zsh:

curl -sSL https://git.io/g-install | sh -s -- zsh
Enter fullscreen mode Exit fullscreen mode

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:

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

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
Enter fullscreen mode Exit fullscreen mode

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

$ g install [version]
Enter fullscreen mode Exit fullscreen mode

You can also install directly the latest version of Go:

$ g install latest
Enter fullscreen mode Exit fullscreen mode

Go installation:

$ g install 1.18.4
Enter fullscreen mode Exit fullscreen mode

Now we can check our current Go version:

$ go version
go version go1.18.4 darwin/arm64
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Conclusion

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

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .