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.
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:
- Create a
.gitpod.yml
touch .gitpod.yml
- Create a custom Dockerfile
touch .gitpod.Dockerfile
- Reference your newly created Dockerfile in your
.gitpod.yml
image:
file: .gitpod.Dockerfile
- Update your
.gitpod.Dockerfile
to install your preferred dependency versions
# 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
- Commit and push both
.gitpod.yml
and.gitpod.Dockerfile
git commit -m "configuring gitpod with go" && git push
- Start a new workspace from the branch with the committed
.gitpod.Dockerfile
For example, opening: gitpod.io/#https://github.com/gitpod-io/gitpod
- Test your dependencies are correct in the new workspace
go version
Note: If your changes are not taking effect, ensure your workspace is building from the correct context, where your
gitpod.yml
orgitpod.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:
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:
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!
So, basically in this video we:
- First, open the Go file that we want to debug
- Then, go to the debug menu and select “Add Configuration…”
- Next, in the dropdown choose “Go launch file”
- 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:
{
// 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:
Debugging your Go applications in GoLand
Steps to debug your Go application in GoLand:
- Open your project in Gitpod with GoLand.
- Open the
main.go
file in the editor. - Click on the
Run
menu and selectEdit Configurations...
. - Click on the
+
button and selectGo Application
. - In the
Go Application
window, enter the name of the configuration and the path to the file you want to debug. - Click on the
Apply
button. - Click on the
Debug
button to start debugging your Go application.
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
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:
---
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. WithcheckoutLocation
andworkspaceLocation
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 | |
go-swagger | A simple yet powerful representation of your RESTful API | |
go-gin-app | Gin example running in Gitpod | |
gosh-terminal | A terminal implemented in Go where you can do anything |
Further Reading
- VS Code/Go Documentation The stuff here also applies to Gitpod!
- JetBrains/GoLand Documentation The stuff here also applies to Gitpod!
- VS Code/Go debugging VS Code’s Documentation on Go debugging