Just Another Golang Intro

k34
The Compiler
Published in
5 min readJul 12, 2016

TL;DR — This is an introduction to Go (also known as Golang), the programming language created by Google in 2008 (released in 2009). This introduction is meant for engineers who are familiar with at least one other language.

Copyright © 2015 Nathan Youngman

I highly recommend Go for a few reasons: even though it is a strongly typed language, its established patterns for type conversion are incredible. The syntax is clean and beautiful, yet surprisingly simple. But, I’m not going to tell you all of the reasons why you should love Go. Since you’re here, you probably know some of those reasons already. So, no more chit chat. Let’s get started. Let’s install Go.

Install Go

Open your terminal and run this command:

$ brew install go

If you don’t have Homebrew installed, simply navigate to this website and follow the instructions.

Now, check to make sure you have Go:

$ go version
> go version go1.6.2 darwin/amd64 # or your Go version

Set your $GOPATH

You need to specify a Go workspace location so Go knows where to run your code. You will do this with the $GOPATH environment variable. Go does everything relative to your $GOPATH so you need to make sure your $GOPATH is configured correctly. We will set this up using your shell profile.

Open your shell profile with Vim:

$ vim .bash_profile

Note: For a crash course on vim check out this cool tutorial called Vimland.

Add the following lines to your shell profile. This will set the $GOPATH to your workspace. The name is not particularly important, but I suggest setting the $GOPATH to either /go or /gocode. As long as you maintain the structure under it, everything should work fine though.

# GOPATH
export GOPATH=”$HOME/go”
export PATH=”$PATH:$GOPATH/bin”

After you’ve saved the file you can quit vim.

Verify your $GOPATH setup by running:

$ echo $GOPATH
> /Users/[USERNAME]/go

Navigating the Go workspace

All Go code is kept in one workspace. A workspace contains many repositories. Repositories contain packages. Packages contain Go source files.

Under the umbrella of your /go workspace, you’ll see three directories:

> bin pkg src

According to How to Write Go Code:

  • src contains Go source files
  • pkg contains package objects
  • bin contains executable commands

Today, we will focus on the src directory, as it is the source of truth for all of the code you will be writing. Let’s go ahead and make a src directory.

$ mkdir src
$ cd src
$ pwd
> /Users/[USERNAME]/go/src

We mentioned repositories earlier that a workspace contains many repositories which contain packages which contain Go source files. Repositories are version controlled, like Git for example.

In the src folder you will have a directory called github.com. Go’s use of URLs as file paths seems strange at first, but it turns out to be incredibly useful when organizing code and navigating the workspace. src will hold all of your Github code that is written in Go, as well as other version control repositories you might have.

To manage our Github code we need to create a directory called github.com in our src folder.

$ mkdir github.com

Now, we can `cd` into the github.com directory to view our wonderful Go repositories. In here, you can either clone an existing repo or create one. For now, we will create a Git repository called another-fancy-repo for us to explore, and start running some fancy Go code.

Running our first Go program

$ cd github.com
$ pwd
> /Users/[USERNAME]/go/src/github.com
$ mkdir fancy-go-repo
$ cd fancy-go-repo

In our fancy-go-repo, we will write, run, build, and test our Go code. For our tutorial we will run our code using `go run`.

Inside the repos, create a main.go file.

$ touch main.go

This will be our main file. Eventually we will have a configuration file (config.go) and packages that we create (like an api/ or a server/ package), but for now let’s just stick to the basics.

Go requires a package name at the top of the file. This specifies which package the file will run from. Because we only have one main file and we only have one package right now, we are in the main package. The entire repository will only have one function called main, and this is run from the main package in the main file.

To demonstrate this, write this in your main.go file:

package mainfunc main() {
fmt.Println("Hello, world!")
}

Now, run the file:

$ go run main.go

Did you get an error that looked something like this?

# command-line-arguments
./main.go:4: undefined: fmt in fmt.Println

Good. We need to import the fmt package to use the Println statement . Go’s import statements are powerful, and come after the package name. fmt is a built-in package so we get it for free. Let’s import it at the top of the file:

package mainimport "fmt"func main() {
fmt.Println("Hello, world!")
}

Now try running the file:

$ go run main.go
> Hello, world!

Great job!

Writing beautiful, error-free code

To ensure error-free code, make sure you run your code through golint and go vet. Also, your editor should run goimports on save.

  • Golint checks to make sure coding style is pretty and makes certain that your code matches the accepted style of the open source Go project (even Google uses it!). It will print out your style mistakes.
  • Go vet is concerned with the correctness of your code. It uses static analysis.
  • Goimports is a tool to fix (add, remove) your Go imports automatically. Goimports will run gofmt for you, which reformats Go source code.

What’s next…

If you want to dive deeper into the wonderful world of Go, I recommend checking out the interactive Tour of Go by the creators of Golang next.

More Resources

50 Shades of Go-Traps, Gotchas, and Common Mistakes for New Golang Devs

Go Data Structures

Go Coding Style Guide

Building a Web Application With Golang

The Go Memory Model

Arrays, slices (and strings): The mechanics of ‘append’

Go Concurrency Patterns: Pipelines and Cancellations

The Best Go Links of 2015

If you enjoyed this Intro to Go please recommend it and share it with others. Post any questions you have below!

Thanks for reading!

--

--