Why Golang?
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
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:
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!
 
 
              

 
    
Top comments (11)
Great :) I'm gonna see second post in this series now.
Really good :) +1 for the nice drawing :p
Going to follow these Go tutorials of yours
Thanks Bamiji :-)
Hi! I also found an alternative to gvm, it's goenv.
You can check it here github.com/syndbg/goenv/
I can't wait to GO through your tutorials :)
janac.me
Going through this right now
Following, Thank you for your work!!!
Thanks Julian 🥰
can't wait for next one. Moar Go!!
got to the end of this with Go installed and now breathless with anticipation.
Thanks Dan
I've just published the second article 😊