DEV Community

loading...
Cover image for Learning Go by examples: Introduction

Learning Go by examples: Introduction

Aurélie Vache
Cloud Developer - Google Developer Expert on Cloud - CKAD - Speaker - Sketchnoter - Technical writer - Conferences organizer - "Duchess France" women in tech association Leader - Mentor
Updated on ・2 min read

Why Golang?

love gopher

Former Java developer for 10 years, I discovered Golang (aka Go) over 4 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 GVM, a Go version manager, that will allow you to install and update the versions of Go by specifying which version you want.

For bash:

bash < <(curl -s-S-L https://raw.githubusercontent.com/moovweb/gvm/master/binscripts/gvm-installer)
Enter fullscreen mode Exit fullscreen mode

For zsh:

zsh < <(curl -s-S-L https://raw.githubusercontent.com/moovweb/gvm/master/binscripts/gvm-installer)
Enter fullscreen mode Exit fullscreen mode

Usage:

$ gvm
Usage: gvm [command]

Description:
  GVM is the Go Version Manager

Commands:
  version    - print the gvm version number
  get        - gets the latest code (for debugging)
  use        - select a go version to use (--default to set permanently)
  diff       - view changes to Go root
  help       - display this usage text
  implode    - completely remove gvm
  install    - install go versions
  uninstall  - uninstall go versions
  cross      - install go cross compilers
  linkthis   - link this directory into GOPATH
  list       - list installed go versions
  listall    - list available versions
  alias      - manage go version aliases
  pkgset     - manage go packages sets
  pkgenv     - edit the environment for a package set
Enter fullscreen mode Exit fullscreen mode

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

$ gvm install [version] [options]
Enter fullscreen mode Exit fullscreen mode

Go installation:

$ gvm install go1.16.5 -B
$ gvm use go1.16.5 --default
Enter fullscreen mode Exit fullscreen mode

In your .zshrc or .bashrc file, set your $GOROOT and $GOPATH environment variables.

Here is an example:

[[ -s "$HOME/.gvm/scripts/gvm" ]] && source "$HOME/.gvm/scripts/gvm"
export GOPATH=$HOME/go
export GOBIN=$GOPATH/bin
export PATH=${PATH}:$GOBIN:$GOROOT/bin
Enter fullscreen mode Exit fullscreen mode

Now we can check our current Go version:

$ go version
go version go1.16.5 darwin/amd64
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!

Discussion (7)

Collapse
bigboybamo profile image
Bamiji Oyetubo

Going to follow these Go tutorials of yours

Collapse
aurelievache profile image
Aurélie Vache Author

Thanks Bamiji :-)

Collapse
julianperezpesce profile image
Julián Pérez Pesce

Following, Thank you for your work!!!

Collapse
aurelievache profile image
Aurélie Vache Author

Thanks Julian 🥰

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

Thanks Dan
I've just published the second article 😊