DEV Community

Cover image for Part 2: Development environment
Jonathan Harrison
Jonathan Harrison

Posted on

Part 2: Development environment

The cover image is from MariaLetta/free-gophers-pack

This post is part of a series detailing my journey with golang from learning the language to entering the DigitalOcean App Platform Hackathon.

The app I built can be found on GitHub.

Part 2 details the setup of my golang development environment.

Setup golang

Install golang

First step is to install golang which can be done by following this page.

Create workspace

This step is not required when using modules. See the note on this page.

Create a workspace which results in the directory structure shown below.

$HOME
    └── go
        ├── bin
        └── src
Enter fullscreen mode Exit fullscreen mode

Set environment variables

Next I added the following environment variables to my .zshrc file.

export GOPATH=/Users/jonjam/go
export GOBIN=/Users/jonjam/go/bin
export PATH=$GOPATH/bin:$GOROOT/bin:$PATH
Enter fullscreen mode Exit fullscreen mode

More information about these variables can be found here.

Setup Visual Studio Code

There are plenty of choices when it comes to IDEs. I went with Visual Studio Code, since I was already familiar with it.

This section details configuring it for golang.

Install and activate Go extension

First to add golang support to VS Code, install the Go extension.

After installing the extension, there are some additional command line tools that also need to be installed to support the extension. This can be done by following this page.

Configure formatting tool

The default formatting tool in VS Code is goreturns which doesn't work with modules (see here for more information).

goimports on the other hand does support modules.

Change the go.formatTool setting to goimports.

Enable the Go language server

Besides formatting, some of the other helper tools do not support modules (see here for more information).

The Go language server should be used; this can be enabled by following these steps.

Setup debugging

Debugging golang apps in VS Code is provided by Delve.

Follow these steps to install and configure Delve.

Setup linter

Now that VS Code is setup, the final step is to choose a linter to assist with development.

I chose golangci-lint. It is a fast go linters runner that is capable of running multiple linters in parralel.

I discovered this on golang wiki.

Install

golangci-lint can be installed locally by following these steps.

Configure linting tool

To integrate golangci-lint with VS Code, it is just a matter of changing a couple of settings.

Create configuration file

The final step is to create a .golangci.yml for the project which defines what linters are enabled. This can also be used to enable/disable specific rules.

Below is the .golangci.yml file from stock-checker app:

linters:
  disable-all: true
  enable:
    # Default enabled linters
    - deadcode
    - errcheck
    - gosimple
    - govet
    - ineffassign
    - staticcheck
    - structcheck
    - typecheck
    - varcheck

    # Added
    # goimports aligns with VS Code setup and includes gofmt
    - goimports
    - golint
Enter fullscreen mode Exit fullscreen mode

Next

That's it for this post.

In part 3, I will talk about developing the project.

Top comments (0)