Go in Gitpod

Gitpod supports Go right out of the box, but there are still ways to optimize your Go experience within Gitpod.

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

Workspace Configuration

VSCode Extensions

Name Description
Go Test Explorer Provides Test Explorer for Go which allows you to run your tests at the click of a button!

To install Go Test Explorer for your repository, add the following to your .gitpod.yml

language icon language: 
yml
vscode:
  extensions:
    - premparihar.gotestexplorer

Start-up tasks

Here is how to have your dependencies automatically fetched before you open your Gitpod workspace!

language icon language: 
yml
tasks:
  - init: go get -v -t -d ./...

A full example of a .gitpod.yml file might look like this

language icon language: 
yml
image: gitpod/workspace-full

tasks:
  - init: go get -v -t -d ./...

vscode:
  extensions:
    - premparihar.gotestexplorer

Using the dep dependency manager in Gitpod

If your project uses the dep 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 brew install 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:
    - premparihar.gotestexplorer

Installing custom go version on a minimal workspace

Let’s say you want go v1.17, follow along! At first, add a .gitpod.Dockerfile file on your repo with the following content in it:

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:2022-04-26-07-40-59

# 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

Secondly, reference the above Dockerfile in your .gitpod.yml configuration file, like so:

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

Now you can See it in action on a new workspace

Debugging

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 enabled, please check out gitpod-io/Gitpod-Go-Debug:

Open in Gitpod

Further Reading

Was this helpful?