Setting up a dev container for Go
- Primary author: Archana Goli
- Reviewer: Shana Tran
Welcome! In this tutorial, you'll learn how to set up a basic Go development container in Visual Studio Code and how to run and compile a simple program.
Why Go?
Go (or Golang) is a popular, modern programming language developed by Google and has gained traction for its simplicity (making it easy to learn and use), compiles to machine code which makes it faster than interpreted languages like Python or JavaScript, and is designed for building scalable and concurrent systems.
Prerequisites
- A GitHub account: Sign up for one here, if you don't have one yet.
- Git installed: Install Git if you don't already have it.
- VS Code: Download and install it here.
- Docker installed: Required to run the dev container. Get Docker here.
- Command-line basics: Your COMP211 command-line knowledge will serve you well here. If in doubt, review the Learn a CLI text!
Part 1: Set up Git Repository
Citations
This section of the tutorial was developed and rewritten from the MkDocs tutorial by Kris Jordan.
Step 1. Create a local directory and initialize Git
- Open your terminal or command prompt
-
Create a new directory for your project. (Note: Of course, if you'd like to organize this tutorial somewhere else on your machine, go ahead and change into that parent directory first. By default this will be in your user's home directory.):
-
Initialize a new Git repository:
- Create a README file:
Step 2. Create a Remote Repository on GitHub
- Log in to your GitHub account and navigate to the Create a New Repository page.
- Fill in the details as follows:
- Repository Name:
comp423-go-tutorial - Description: Tutorial of Go, a statically typed, compiled high-level general purpose programming language developed by Google.
- Visibility: Public
- Repository Name:
- Do not initialize the repository with a README, .gitignore, or license.
- Click Create Repository.
Step 3. Link your Local Repository to GitHub
- Add the GitHub repository as a remote:
Replace
<your-username>with your GitHub username. - Check your default branch name with the subcommand
git branch. If it's not main, rename it to main with the following command:git branch -M main. Old versions ofgitchoose the namemasterfor the primary branch, but these daysmainis the standard primary branch name. - Push your local commits to the GitHub repository:
git push --set-upstream origin main
Why do we do this?
The flag -set-upstream sets up the main branch to track the remote branch, so that future pushes and pulls made can be done without specifying the branch name and you can just write git push origin when working on your local `main branch.
- Back in your web browser, refresh your GitHub repository to see that the same commit you made locally has now been pushed to remote. You can use
git loglocally to see the commit ID and message which should match the ID of the most recent commit on GitHub. This is the result of pushing your changes to your remote repository.
Part 2: Setting up the Development Environment
What is a Development (Dev) Container?
Citations
This section of the tutorial was developed and rewritten from the MkDocs tutorial by Kris Jordan.
A dev container ensures that your development environment is consistent and works across different machines. At its core, a dev container is a preconfigured environment defined by a set of files, typically leveraging Docker to create isolated, consistent setups for development. Think of it as a "mini computer" inside your computer that includes everything you need to work on a specific project—like the right programming language, tools, libraries, and dependencies.
Why is this valuable? In the technology industry, teams often work on complex projects that require a specific set of tools and dependencies to function correctly. Without a dev container, each developer must manually set up their environment, leading to errors, wasted time, and inconsistencies. With a dev container, everyone works in an identical environment, reducing bugs caused by "it works on my machine" issues. It also simplifies onboarding new team members since they can start coding with just a few steps.
How are software project dependencies managed?
To effectively manage software dependencies, it's important to understand package and dependency management. In most software projects, you rely on external libraries or packages to save time and leverage work that has already been done by others. Managing these dependencies ensures that your project has access to the correct versions of these libraries, avoiding compatibility issues.
In summary, the devcontainer.json file specifies configuration for a consistent development environment using a Docker image.
Now let's establish the development environment for Go.
Step 1. Add Development Container Configuration
- In VS Code, open the
comp423-go-tutorialdirectory. You can do this via: File > Open Folder. - Install the Dev Containers extension for VS Code.
- Create a
.devcontainerdirectory in the root of your project with the following file inside of this "hidden" configuration directory. Thedevcontainer.jsonfile defines the configuration for your development environment. Here, we're specifying the following:- name: A descriptive name for your dev container.
- image: The Docker image to use, in this case, the latest version of a Go environment.
- customizations: Adds useful configurations to VS Code, like installing the Go extension. When you search for VSCode extensions on the marketplace, you will find the string identifier of each extension in its sidebar. Adding extensions here ensures other developers on your project have them installed in their dev containers automatically.
- postCreateCommand: A command to run after the container is created. In our case, it will run
go mod init github.com/<your-username>/comp423-go-tutorial. Thego mod initsubcommand creates a go.mod file to track your code's dependencies. Currently, the file contains the name of your module and the Go version your dev container supports. But as you add dependencies, the go.mod file will list the versions your code depends on.Replace{ "name": "COMP423 Go Tutorial", "image": "mcr.microsoft.com/devcontainers/go:latest", "customizations": { "vscode": { "settings": {}, "extensions": ["golang.go"] } }, "postCreateCommand": "go mod init github.com/<your-username>/comp423-go-tutorial || true" }<your-username>with your GitHub username.
Step 2. Reopen the Project in a VSCode Dev Container
Reopen the project in the container by pressing Ctrl+Shift+P (or Cmd+Shift+P on Mac), typing "Dev Containers: Reopen in Container," and selecting the option. This may take a few minutes while the image is downloaded and the requirements are installed.
Once your dev container setup completes, close the current terminal tab (trash can), open a new terminal pane within VSCode, and try running go version to see your dev container is running a recent version of Go without much effort! (As of this writing: 1.25 released in January of 2025.)
Part 3: Creating, running, and compiling a Hello world program
Citations
Material was adapted from the official Go tutorial.
Step 1. Creating the project
In your root directory, create a file hello.go in which to write your code.
Step 2. Write the program
Paste the following code into your file:
Let's break the code down:- package main: declares a
mainpackage (a package is a way to group functions, and it’s made up of all the files in the same directory) - import “fmt”: import the
fmtpackage, which contains functions for formatting text, including printing to the console. This package is one of the standard library packages that come with installing Go. - main(): Implementation of the main function that will print a message to the console. A main function executes by default when you run the main package.
Step 3. Run your code
Run the program using the command go run . in your terminal.
the . in go run .
The . signifies the current directory. go run . compiles and runs the named main Go package in the current directory.
This compiles and runs the Go package main.
Step 4. Compile your code
While the go run command is a useful shortcut to compile and run and program when making frequent changes, it doesn’t generate a binary executable. The go build command compiles the packages, along with their dependencies.
What about installing the results?
go build doesn’t install the results. The go install command will compile and install the packages.
- From the command line in the root directory, run the
go buildcommand to compile the code into an executable. - Run the new executable directly in the terminal.
go runboth compiles and runs the program in one step, but doesn’t produce a separate executable file. You would typically usego runwhen you’re developing and want to quickly test a Go program without needing to create a standalone executable.go buildwill compile and generate an executable file, which you can run separately. You would typically usego buildwhen you want to create a distributable binary that can be executed independently or when preparing your program for deployment.
Comparision to gcc
Go build may feel familiar as the build command is similar to the gcc subcommand for C and C++, which combines relocatable object files and libraries into a binary executable object file that can be copied into memory and ran.
Step 5. Push Changes
- Add and commit your changes
- Push the changes to GitHub:
Conclusion
Congratulations! You've successfully set up a dev container for Go and compiled and ran a Hello 423 program. If you would like to explore Go further and learn how to create and import modules, you can find more information in the Go documentation.