Setting up a dev container for Go
- Primary author: Hugh Toomey
- Reviewer: Cem Baykal
- note: some content from this tutorial is reused from an mkDocs tutorial available here.
Features in this tutorial
- Code Blocks
Code Blocks allow for code snippits to be highlighted and easily copied or explained. We will use codeblocks to highlight commands and code snippets to make this tutorial easy to follow along. - Admonitions
Admonitions will be used to provide extra context for any steps within this tutorial
Note
This is an example of an Admonition.
Prerequisits
- A GitHub account: If you don’t have one yet, sign up at GitHub.
- Git installed: Install Git if you don’t already have it.
- Visual Studio Code (VS Code): Download and install it from 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: Create your repository
-
Create a Local Directory and Initialize Git
- Open your terminal or command prompt.
- Create a new directory for your project.
- Initialize a new Git repository:
- Create a README file:
- 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:
go-project - Description: "My first project with Go"
- Visability: Public
- Repository Name:
- Do not initialize the repository with a README, .gitignore, or license.
- Click Create Repository
- 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 of git choose the name master for the primary branch, but these days main is the standard primary branch name.
- Push your local commits to the GitHub repository:
- 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
- Add Development Container Configuration
- In VS Code, open the
go-projectdirectory. 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: Paste this content into your `devcontainer.json' Explanation name: The label that appears in your VS Code Dev Container environment.image: Points directly to an existing Docker image—in this case, the official Go image on Docker Hub (golang:1.20).- If you need a different version, you can change the tag (e.g., golang:1.19).
settings: Custom VS Code settings inside the container (e.g., go.gopath, default shell).extensions: Lists extensions that will be installed automatically in the container. For Go development, golang.go is essential.postCreateCommand: Runs after the container is created. Here, go mod tidy cleans up any dependencies.
- In VS Code, open the
-
Reopen the Project in a VSCode Dev Container
Reopen the project in the container by pressingCtrl+Shift+P(orCmd+Shift+Pon 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!
Usego versionin your terminal to confirm that you have a semi recent version of Go installed (go version go1.20.14 linux/amd64 as of Jan/26/2025) !!! note title="Why Go?" Go is an open-source language developed by Google in 2007. It was designed to be simple and efficient. Go is used greatly in web-servers and cloud-services.
Part 3: Creating your own project
- Make a new directory called
hello - Initialize your go module
-
Add a new file in the hello directory called
!!! note title="Why main?" While amain.goand write in your first programmainfunction is not required in Go butfunc main() {}is required for executables. -
Now its time to run your program! This can be done with two options: 1. Run your program directly in your terminal
* Compiles your Go code in memory and immediately executes it. * No standalone binary is produced (the compiled program is temporary). 2. Create an executable file using the build command. This command creates an file that can be run directly * Compiles your code into an executable file (e.g.,mainormain.exe) * Does not automatically run the program; you run the binary separately afterward.
Thats it!
congratulations on completing your first go project!