Hi folks! It’s been a long time since I have published the last post, but now I came back with short quickstart guide in Go.

In this tutorial, we will configure Go environment in VS Code and write our first program in Go.

Install Go

The first thing that you need to do it’s to install Go on your computer. To do so, download installer for your operating system from here and then run the installer.

Configure GOPATH

By language convention, Go developers store all their code in a single place called workspace. Go also puts dependency packages in the workspace. So, in order to Go perform correctly, we need to set GOPATH variable with the path to the workspace.

MacOS and Linux

Set the GOPATH envar with workspace

export GOPATH=$HOME/go

Also, we need to add GOPATH/bin to PATH in order to run compiler Go programs:

export PATH=$PATH:$GOPATH/bin

Configure VS Code

Install official Go extension.

Install delve debugger:

go get -u github.com/derekparker/delve/cmd/dlv

I recommend you to add the following lines to your VS Code user settings:

settings.json
1
2
3
4
{
    "go.autocompleteUnimportedPackages": true,
    "go.formatTool": "gofmt"
}

Windows

Create GOPATH envar:

set GOPATH=c:\Users\%USERNAME%\go

Also, we need to add GOPATH\bin to PATH in order to run compiler Go programs:

set PATH=%PATH%;%GOPATH%\bin

Create project

Move to your GOPATH/src directory. Create a directory for your project:

cd $GOPATH/src
mkdir -p github.com/alikhil/hello-world-with-go

Open it using vscode:

code github.com/alikhil/hello-world-with-go

Hello World!

Let’s create a file named program.go and put the following code there:

program.go
1
2
3
4
5
6
7
package main

import "fmt"

func main() {
    fmt.Println("¡Hola, mundo!")
}

Run the program

Finally, to run the program by pressing the F5 button in VS Code and you should see the message printed to Debug Console.

That’s all! My congratulations, you have just written your first program in Go!

Troubleshooting

If you fail to run your program and there is some message like “Cannot find a path to go. Try to add to your PATH envar with path directory where go binary is stored.

For example in MacOS I have added following line to my ~/.bash_profile:

export PATH=/usr/local/go/bin:$PATH