Bit Hacker Logo
Bit Hacker Logo
Install and Setup Go for Development on Mac or Windows

Install and Setup Go for Development on Mac or Windows

Golang has been around for some time and has attracted a lot of attention for its speed and simple cross machine builds. If you are looking at using it in your next project or simply trying it out this guide will help you set it up. It will also address the important directory setup.

Download Go

Head over to golang.org and download the latest version, using the installer for your OS.

The GOPATH

With Go now installed there is something important to understand about the GOPATH. As in many languages, you install and start coding in whichever directory you wish. When you install dependencies, they get installed beside your application in their respective package folders.

your-project
|- node_modules
  |- package
|- main.js

Go takes a different approach.

To avoid having 5 projects that duplicating the same dependencies, Go has you develop your application in the root GOPATH under your projects own folder or namespace. These folders typically get filled under a publicly accessible url format to allow the importer to know where to download the package from when installing dependencies.

GOPATH/src
|- package
|- your-project
  |- main.go

You can select any location for your GOPATH but there must only be one.

Your first Go application

Let's create your first Go application. Inside GOPATH/src create a directory called hello. Inside make a file called hello.go.

package main

import "fmt"

func main() {
  fmt.Println("Hello!")
}

Open a terminal or command prompt and cd into your GOPATH/src/hello folder and run the following and you should see the output below.

$ go run .
Hello!

To build your application:

$ go build .

Run the built binary:

$ hello
Hello!

You just made your first Go application! For more in-depth information on writing go, or working with the structure, Golang.org has fantastic documentation.

IDE's

Go has support within a variety of IDE's. For professional support and in-depth features you can try JetBrain's commericial IDE Goland which starts at $199. For just trying out Go, you can get Visual Studio Code which has create plugins to help with Go development or Atom.

All IDE's listed above have Mac and Windows versions.