DEV Community

Cover image for Learning Go by examples: part 1 - Introduction & Installation
Aurélie Vache
Aurélie Vache

Posted on • Edited on

Learning Go by examples: part 1 - Introduction & Installation

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

> 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!

Top comments (11)

Collapse
 
lico profile image
SeongKuk Han

Great :) I'm gonna see second post in this series now.

Collapse
 
richarddevers profile image
Richard Devers

Really good :) +1 for the nice drawing :p

Collapse
 
bigboybamo profile image
Olabamiji Oyetubo

Going to follow these Go tutorials of yours

Collapse
 
aurelievache profile image
Aurélie Vache

Thanks Bamiji :-)

Collapse
 
hasobi profile image
Hasobi

Hi! I also found an alternative to gvm, it's goenv.
You can check it here github.com/syndbg/goenv/

Collapse
 
julianperezpesce profile image
Julián Pérez Pesce

Following, Thank you for your work!!!

Collapse
 
aurelievache profile image
Aurélie Vache

Thanks Julian 🥰

Collapse
 
sxmmie profile image
Samuel Umoh

Going through this right now

Collapse
 
janacm profile image
janac meena • Edited

I can't wait to GO through your tutorials :)

janac.me

Collapse
 
arroyoruy profile image
Dan Arroyo

can't wait for next one. Moar Go!!

got to the end of this with Go installed and now breathless with anticipation.

Collapse
 
aurelievache profile image
Aurélie Vache

Thanks Dan
I've just published the second article 😊