Go in Gitpod

Gitpod includes Go in the default image, but if you need to customize your Go version or IDE setup in Gitpod, this guide will help you.

Prerequisites

This guide assumes familiarity with:

Docker, YAML, Linux, Bash and Linux environment variables.

Getting started / Quick Start

To see a full working Go application, take a look at gitpod-samples/template-golang-cli. To update an existing Go application, follow the steps below in this guide.

Push

Installing Dependencies

The default base image

The default Gitpod workspace image default is workspace-full based on Ubuntu.

This base image includes:

  • Go v1.19.1 (go version)

Note: We discuss how to set up a custom base image later in the guide.

Updating Go Versions

Gitpod uses the latest stable version of Go by default. If you want to use a different version, you can use the Go Version Manager to install and manage multiple versions of Go or you can following their official guide.

Setting up a custom Dockerfile

To ensure Gitpod workspaces always start with the correct dependencies, configure a Dockerfile:

  1. Create a .gitpod.yml
language icon language: 
bash
touch .gitpod.yml
  1. Create a custom Dockerfile
language icon language: 
bash
touch .gitpod.Dockerfile
  1. Reference your newly created Dockerfile in your .gitpod.yml
language icon language: 
yml
image:
    file: .gitpod.Dockerfile
  1. Update your .gitpod.Dockerfile to install your preferred dependency versions
language icon language: 
dockerfile
# You can find the new timestamped tags here: https://hub.docker.com/r/gitpod/workspace-base/tags
FROM gitpod/workspace-base:latest

# Change your version here
ENV GO_VERSION=1.17

# For ref, see: https://github.com/gitpod-io/workspace-images/blob/61df77aad71689504112e1087bb7e26d45a43d10/chunks/lang-go/Dockerfile#L10
ENV GOPATH=$HOME/go-packages
ENV GOROOT=$HOME/go
ENV PATH=$GOROOT/bin:$GOPATH/bin:$PATH
RUN curl -fsSL https://dl.google.com/go/go${GO_VERSION}.linux-amd64.tar.gz | tar xzs \
    && printf '%s\n' 'export GOPATH=/workspace/go' \
                      'export PATH=$GOPATH/bin:$PATH' > $HOME/.bashrc.d/300-go
  1. Commit and push both .gitpod.yml and .gitpod.Dockerfile
language icon language: 
bash
git commit -m "configuring gitpod with go" && git push
  1. Start a new workspace from the branch with the committed .gitpod.Dockerfile

For example, opening: gitpod.io/#https://github.com/gitpod-io/gitpod

  1. Test your dependencies are correct in the new workspace
language icon language: 
bash
go version

Note: If your changes are not taking effect, ensure your workspace is building from the correct context, where your gitpod.yml or gitpod.Dockerfile are checked in to version control and are on the branch or commit that you are opening.

See configure Docker for more.

Using the dep dependency manager in Gitpod

If your project uses the dep (deprecated - v0.5.4) dependency manager then you need to add a .gitpod.Dockerfile to your project. A basic example that extends the default workspace image might be something like:

language icon language: 
dockerfile
FROM gitpod/workspace-full

USER gitpod

RUN sudo apt-get install go-dep

Also, don’t forget to reference the above Dockerfile in your .gitpod.yml configuration file, like so:

language icon language: 
yml
image:
    file: .gitpod.Dockerfile

tasks:
    - init: dep ensure

vscode:
    extensions:
        - golang.go
        - premparihar.gotestexplorer

Debugging your Go application in Gitpod

Debugging your Go applications in VS Code

Here is a quick clip on how to automatically configure debugging for Go!

Go debugging example

So, basically in this video we:

  1. First, open the Go file that we want to debug
  2. Then, go to the debug menu and select “Add Configuration…”
  3. Next, in the dropdown choose “Go launch file”
  4. Finally, start debugging your Go program!

You can also create the Go debug configuration file manually

To start debugging your Go application in Gitpod, please create a new directory called .theia/, and inside add a file called launch.json, finally, add the following to it:

language icon language: 
json
{
	// Use IntelliSense to learn about possible attributes.
	// Hover to view descriptions of existing attributes.
	"version": "0.2.0",
	"configurations": [
		{
			"name": "Launch file",
			"type": "go",
			"request": "launch",
			"mode": "debug",
			"program": "${file}"
		}
	]
}

Then, simply open the Go file you want to debug, open the Debug panel (in the left vertical toolbar, click the icon with the crossed-out-spider), and click the green “Run” button.


To see a basic repository with Go debugging, please check out gitpod-samples/template-golang-cli:

Open in Gitpod

Debugging your Go applications in GoLand

Steps to debug your Go application in GoLand:

  1. Open your project in Gitpod with GoLand.
  2. Open the main.go file in the editor.
  3. Click on the Run menu and select Edit Configurations....
  4. Click on the + button and select Go Application.
  5. In the Go Application window, enter the name of the configuration and the path to the file you want to debug.
  6. Click on the Apply button.
  7. Click on the Debug button to start debugging your Go application.
Debug on GoLand in Gitpod

Using $GOPATH

Older Go projects without module support need a specific workspace layout: the source code of your repository and its dependencies must be in the directories

language icon language: 
bash
src/[repository-provider]/[repository-owner]/[repository-name]

in the $GOPATH. Using the .gitpod.yml file, you can bring about such a workspace layout. Here is how we do that for the example go-gin-app repository:

language icon language: 
yml
---
checkoutLocation: 'src/github.com/demo-apps/go-gin-app'
workspaceLocation: '.'
tasks:
    - init: |
          cd /workspace/src/github.com/demo-apps/go-gin-app &&
          go get -v ./... &&
          go build -o app
      command: |
          cd /workspace/src/github.com/demo-apps/go-gin-app &&
          ./app

In more detail:

  • By default, Gitpod clones the repository into the directory /workspace, which becomes the root directory for the workspace. With checkoutLocation and workspaceLocation you can change this behavior (the paths are taken relative to /workspace).
  • Gitpod preconfigures the $GOPATH environment variable to include the directory /workspace/go.
  • With go get -v ./... we retrieve the sources of the dependencies from GitHub.
  • To build the app, we run go build -o app.
  • Lastly, we start the application.

Example Repositories

Here are a few Go example projects that are already automated with Gitpod:

Repository Description Try It
prometheus The Prometheus monitoring system and time series database Open in Gitpod
go-swagger A simple yet powerful representation of your RESTful API Open in Gitpod
go-gin-app Gin example running in Gitpod Open in Gitpod
gosh-terminal A terminal implemented in Go where you can do anything Open in Gitpod

Further Reading

Was this helpful?