Golang Basics - Setup and Installation

Before we get started, let's install Go, and go through the folder structure and some of its conventions.


Go is a fairly easy language to get started with. You can find installation instructions for the OS of your choice in the official documentation page.

The Go command line tools make use of git as well, so you may want to install that, if you haven't already

Once you complete the installation, the Go command line tools should work as expected. You can test your installation by running go version. Here's the output I got on my Mac:

$ go version
go version go1.12 darwin/amd64

The GOPATH folder

Now, there's one folder that is really important when developing in Go, and that is your GOPATH folder. You can find out the location of your GOPATH by running the command:

$ go env

This will output a bunch of information, one of which is your GOPATH folder.

If you look inside this folder, you will find three more folders: src, bin, and pkg

If you don't find these folders, you can go ahead and create them yourself

Folder structure

The src folder is where all your Go code will reside. In most other languages, you can create your project in any folder on your computer. With Go, it's best to have all your code inside the src directory.

Go actually enforces a folder structure where your project path is derived from the location of your projects online repository. For example, I have a project hosted on https://github.com/sohamkamani/detective: This means the path of the project on my computer will be: $GOPATH/src/github.com/sohamkamani/detective

If I want to make a new project called loremipsum and I want to host it on my github profile as well, then the path to my project folder would be: $GOPATH/src/github.com/sohamkamani/loremipsum

Now, if you're not planning to host your project anywhere online, you could technically just create your project folder anywhere inside the src directory.

The pkg folder, as the name suggests, is used to hold package objects. Package objects are used during compilation, to make the final binary file. You can think of the stuff in the pkg folder as intermediate objects in the overall compilation step. You won't be using this folder that much, so you don't need to pay too much attention to it.

The bin folder will contain all the executable binaries that are created after compiling your Go programs.

From this point on, all folders mentioned will be relative to the GOPATH. So src/a/b/c actually means $GOPATH/src/a/b/c

Your first Go program

Create a new folder for your project:

mkdir src/<your_repository_path>/golang-basics

<your_repository_path> will be replaced by your personal repository path. For me, its github.com/sohamkamani, which makes my folder path src/github.com/sohamkamani/golang-basics

Within your project folder, create a new file called main.go:

// Every Go file needs to have a package declaration
package main

// The import statement here imports the fmt library, which can be accessed via
// the `fmt` symbol
import "fmt"

// Every Go program needs to have a `main` function. This is the first piece of
// code that will run once your binary executes
func main(){
        // The `Println` function just prints its contents to the console
        fmt.Println("hello world")

To execute this code, we need to compile it first. After running the command:

$ go build

A binary file called golang-basics (same as the name of the folder) will be created.

We can then run it and see the result:

$ ./golang-basics
hello world

📖 Read next: Variables

Liked this article? Share it on: