Go

Go (also known as Golang) is an open source programming language maintained by Google.

Installation

  1. $ brew install golang

When installed, try to run go version to see the installed version of Go.

Setup your workspace

Add environment variables

Go has a unique approach of managing code where you have a single workspace for all your Go projects. For more information see the documentation.

First, you’ll need to tell Go the location of your workspace. We’ll do this by adding some environment variables in your shell config file (usually .bash_profile, .bashrc or .zshrc).

  1. export GOPATH=$HOME/go
  2. export GOROOT=/usr/local/opt/go/libexec
  3. export PATH=$PATH:$GOPATH/bin
  4. export PATH=$PATH:$GOROOT/bin

Create your workspace

Create the workspace directories tree:

  1. $ mkdir -p $GOPATH $GOPATH/src $GOPATH/pkg $GOPATH/bin

$GOPATH/src This is where your Go projects are located
$GOPATH/pkg A folder that contains every package objects
$GOPATH/bin The compiled binaries home

Write your first program

Create a file in your $GOPATH/src, for example hello.go, and input the following code

  1. package main
  2. import "fmt"
  3. func main() {
  4. fmt.Printf("hello, world\n")
  5. }

Run the program by running:

  1. $ go run hello.go

If you wish to compile it and move it to $GOPATH/bin, then run:

  1. $ go install hello.go

Since we have $GOPATH/bin added to our $PATH, you can run your program from anywhere:

  1. $ hello

Import a Go package

Besides creating your own packages you can import and use other packages in your Go code. To do so you’ll use the go get command:

  1. $ go get -u github.com/gorilla/mux

The command above will import the package mux into this directory $GOPATH/src/github.com/gorilla/mux.

You can then use this package in your Go programs like this:

  1. package main
  2. import (
  3. "net/http"
  4. "log"
  5. "github.com/gorilla/mux" // Your imported package
  6. )
  7. func YourHandler(w http.ResponseWriter, r *http.Request) {
  8. w.Write([]byte("Gorilla!\n"))
  9. }
  10. func main() {
  11. r := mux.NewRouter()
  12. // Routes consist of a path and a handler function.
  13. r.HandleFunc("/", YourHandler)
  14. // Bind to a port and pass our router in
  15. log.Fatal(http.ListenAndServe(":8000", r))
  16. }

Format your code

Go has a built-in tool that automatically formats Go source code.

To format a single file run:

  1. $ gofmt -w yourcode.go

You can also format an entire package (Note that the command is different from formatting a single file):

  1. $ go fmt path/to/your/package

Generate documentation

With the godoc command you can generate documentation from your code and read documentation from other packages.

  1. $ godoc fmt # documentation for package fmt
  2. $ godoc fmt Printf # documentation for fmt.Printf
  3. $ godoc -src fmt # fmt package interface in Go source form

You need to respect some spec in order to document using godoc. More information in the Godoc documentation.

Learn more

This interactive tutorial will let you learn more about Go.