Go (or golang) is a programming language created at Google in 2007 by Robert Griesemer, Rob Pike, and Ken Thompson. It is a compiled, statically-typed language in the tradition of Algol and C.
Go has garbage collection, limited structural typing, memory safety, and CSP-style concurrent programming features added. The compiler and other language tools originally developed by Google are all free and open source.
Go's popularity is increasing fast. It is a great choice for building web applications.
Now let's see how to install and get started with Go.
Install Golang with Homebrew:
$ brew update $ brew install golang
Installing Go on MacOS using a tarball
Link to tarball
You can get the link to the MacOS tarball archive from the Latest Stable section of the golang download page.
In this installation process we’ll use the latest stable version as of this writing (go 1.9.1). For a newer or older version simply replace the link in the first step. Check the golang download page to see what versions are currently available.
Installing Go 1.9.1
$ curl -O https://storage.googleapis.com/golang/go1.9.1.darwin-amd64.tar.gz $ sudo tar -C /usr/local -xzf go1.9.1.darwin-amd64.tar.gz $ export PATH=$PATH:/usr/local/go/bin
Install Golang on Ubuntu with apt
Using Ubuntu’s Source Package Manager (apt) is one of the easiest ways to install Go. You won’t get the latest stable version, but for the purpose of learning this should be enough.
$ sudo apt-get update $ sudo apt-get install golang-go
Check the installation and version of Go
To check if go was successfully installed, run:
$ go version > go version go1.9.1 linux/amd64
This will print the version of Go that's installed to the console. If you see a version of Go, you'll know that the installation went smoothly.
Setup the workspace
Add Environment variables:
First, you’ll need to tell Go the location of your workspace.
We’ll add some environment variables into shell config. One of does files located at your home directory bash_profile, bashrc or .zshrc (for Oh My Zsh Army)
$ vi .bashrc
then add those lines to export the required variables
This is actually your .bashrc file
export GOPATH=$HOME/go-workspace # don't forget to change your path correctly! export GOROOT=/usr/local/opt/go/libexec export PATH=$PATH:$GOPATH/bin export PATH=$PATH:$GOROOT/bin
Create your workspace
Create the workspace directories tree:
$ mkdir -p $GOPATH $GOPATH/src $GOPATH/pkg $GOPATH/bin $GOPATH/src : Where your Go projects / programs are located $GOPATH/pkg : contains every package objects $GOPATH/bin : The compiled binaries home
For a quickstart and boilerplate Go project, try Alloy.
- Clone Alloy repository
git clone https://github.com/olliecoleman/alloy cd alloy
2. Install the dependencies
glide install npm install
3. Start the development server
go install alloy dev
4. Visit website at
Alloy uses Node, NPM, and Webpack.
The Golang Playground
Learning how to install Go on your local machine is important. But if want to start playing with Go right in your browser, then Go Playground is the perfect sandbox to get started right away!
Just open a new browser window and go to the Playground.
Once there you’ll get the buttons:
The Run button just sends the instructions for compiling the code you wrote to the Google servers that run the Golang back end.
The Format button implements the idiomatic formatting style of the language. You can read more about it here.
Imports just check what packages you have declared within import(). An import path is a string that uniquely identifies a package. A package’s import path corresponds to its location inside a workspace or in a remote repository (explained below). You can read more here.
With Share you get a URL where the code you just wrote is saved. This is useful when asking for help showing your code.
A map, called a dictionary in other languages, “maps” keys to values. A map is declared like this:
var m map[Key]Value
This map has no keys and no keys can be added to it. To create a map, use the
m = make(map[Key]Value)
Anything can be used as a key or as a value.
Here are some common action with maps.
Create or change element
foo in map
m["foo"] = bar
Get element with key
foo in map
element = m["foo"]
Delete element with key
foo in map
Check if a key has been used
Check if key
foo has been used in map
element, ok = m["foo"]
true, the key has been used and
element holds the value at
false, the key has not been used and
element holds its zero-values.
You can directly create map literals: