Go - Hello world, linting and build problems

2018-10-09

Go - Hello world, linting and build problems

Go is a programming language that is compiled, statically typed, expressive, concise and clean. It throws away things like inheritance in favour of composition. Its efforts at having a unified style caught my attention and made me eager to learn Go. It's time to act! I will be learning Go and posting those things I think are worth sharing.

Installing Go

This way of installing Go will enable you to have multiple versions at the same time and switch between them using environment variables. Plus you won't need root access which is great. Instead of installing Go at /usr/local as recommended in the official getting started, we will install it in our HOME.

mkdir -p $HOME/.golang/1.11.1/
cd $HOME/.golang/
curl -O https://dl.google.com/go/go1.11.1.linux-amd64.tar.gz

# --strip-components will remove the top folder that contains the code, "go"
tar -C $HOME/.golang/1.11.1 --strip-components=1 -xzf go1.11.1.linux-amd64.tar.gz

Setting two environment variables will make our installation to work properly:

export GOROOT=$HOME/.golang/1.11.1
export PATH=$PATH:$GOROOT/bin

Now you should be able to run go and see the help printed on terminal.

Preparing your environment

Go works using the GOPATH variable, so each different GOPATH is an isolated environment much like Python's virtualenvs. Let's set ours and install the official Go linter, golint.

export GOPATH=$HOME/go-projects/getting-started/
mkdir -p $GOPATH
cd $GOPATH
go get -u golang.org/x/lint/golint

The linter will help us to write better code and comply with style standards. The golint executable is at $GOPATH/bin so if you want to run it directly you can add that directory to your PATH or run $GOPATH/bin/golint.

Writing some code!

Where we should place our Go code? After installing golint a src folder appeared inside the GOPATH. There is where you should place your project. I will be calling mine go-hello-world so its path is $GOPATH/src/go-hello-world. If you want to share it, the best practice is to place it at the route where the public repository will live. In my case would be $GOPATH/src/gitlab.com/hectormartinez/go -hello-world.

mkdir $GOPATH/src/go-hello-world
cd $GOPATH/src/go-hello-world

Now open the folder in your favourite editor. Create a file named main.go and paste the hello world code on it:

package main

import "fmt"

func main() {
   fmt.Printf("hello, world\n")
}

Then run go build and an executable will be created into the directory. Run now ./go-hello-world and it will print to the stdout on your terminal. You can run go install and the executable will be placed at $GOPATH/bin.

$ ./go-hello-world  # Or go-hello-world
hello, world

Playing with our file

Getting familiar with errors of a programming language is really useful when you have to debug code, so break your 'hello world' file! Introduce or remove statements or letters and try to compile it again. I made a few:

  1. Removing import "fmt" I get:

    $ go build
    # go-hello-world
    ./main.go:4:2: undefined: fmt
  2. Removing package main I get:

    $ go build -v
    can't load package: package go-hello-world:
    main.go:1:1: expected 'package', found 'import'
  3. Removing the closing parenthesis on the fmt.Printf function I get:

    $ go build
    # go-hello-world
    ./main.go:6:29: syntax error: unexpected newline, expecting comma or )

As the files become more complex, some errors can hide others, an error can be caused lines before, etc. So be creative and break your own code!

Linting and golint

If we lint our main.go file no message will be printed, our code is styled nicely. Good job! Now try to make golint complain about your file. However, it should compile without problem using go build or go lint. After playing a little I found that if we change main by Main, the linter complains saying:

$ golint main.go
main.go:1:1: don't use MixedCaps in package name; Main should be main
main.go:5:1: exported function Main should have comment or be unexported

This is a simple file and it's difficult to create linter problems maintaining the go compiler happy. For styling Go you can check Effective Go.

Summary

That's all for now! Now we know how to install Go, how to create an "environment", how to make sad the Go compiler and how to lint our code! The next post about Go I will be exploring its types, creating some functions and testing them with the built-in test framework.

programming language programming basics