Setting Up a Dev Container for Go
Primary author: Lixin Yang (https://github.com/lixiny1)
Reviewer: Ezra Heinberg (https://github.com/ezra45)
Prerequisites
Software Requirements
- Create a Github account and make sure Git is installed
- Install Visual Studio Code
- Install Docker and make sure it's running
Basic Knowledge
- Make sure to be familiar with command line basics! (COMP 211)
- Some basic understanding of Go and its syntax would be useful
Part 1: Creating a New Directory
- Open a terminal on your laptop
- Create a new blank directory and switch to it:
- Initialize a new Git repository. This will initialize your folder with a new, empty repository!
-
Create a remote repository in Github (instructions by Kris Jordan)
- Log into your Github account and go to the Create a New Repository page
-
Fill in these details:
- Repository Name:
go-tutorial - Description: "A short program written in Go!"
- Visibility: Public
- Repository Name:
-
Click Create Repository.
-
Link your Local Repository to GitHub
-
Replace
<your-username>with your GitHub username. -
Check your default branch name with the subcommand
git branch. If it's notmain, rename it tomainwith the following command:git branch -m main. -
Push your local commits to the GitHub repo:
Info
What is --set-upstream?
git push --set-upstream origin main pushes the main branch to the remote repository origin. The --set-upstream flag sets up the main branch to track the remote branch, meaning future pushes and pulls can be done without specifying the branch name and just writing git push origin when working on your local main branch. The corresponding short flag is -u.
- 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.
Part 2: Creating a Dev Container
- Open up the newly created
hello-comp423directory in VS Code through File -> Open Folder - Press
Ctrl-Shift-P(orCmd-Shift-Pif on Mac) and search for Dev Containers: Add Dev Container Configuration Files - Select Add configuration to workspace and select Go from the dropdown menu. This will add a
devcontainer.jsonfile to your folder!
Part 3: Configuring the Dev Container File
- Take a look at the
devcontainer.jsonfile.- "name" refers to the name of your Dev Container. In this case, the name is "Go Dev Container".
- "image" refers to the base image from Microsoft with Go installed.
- Under extensions, we specify for the
golang.goVSCode plugin to be installed. This will allow us to work with.gofiles within VSCode. - "postCreateCommand" will run
go versionand tell you what version of Go is running after you create the container and run this file. - Here is an example of what your
devcontainer.jsonfile should look like:
Note
Make sure to double check that you are installing the official Go VSCode Plugin! The code above should download the correct version.
Part 4: Creating Go Module
- Open up a terminal in VSCode within the Dev Container and run the following command:
This command sets up the current directory as the root of a new Go module. The
go.modfile is the center of this module and allows you to modify any dependencies that you may have.
Part 5: Creating Your First Program!
- Within your dev container, create a new directory called docs. This is where we will be storing our Go programs.
- Create a new Go file called main.go within the docs directory. This file will contain our first Go program!
Note
The main package in Go is special because it defines the entry point into the program.
-
Navigate to the main.go file.
-
package maindescribes the package your code is in -
fmtis a package that we are instructing to install when this program runs. fmt is a standard library package in Go used in formatting I/O, such as printing to a console. -
func maindescribes the main function within this particular Go program.funcis the function declaration keyword, andmainis the name of the function that are running. fmt.Println("Hello COMP423)will utilize thefmtpackage you downloaded to print "Hello COMP 423" to your console as output.
-
-
Verify your Go version with the following command in your terminal to make sure that you're running the correct version
-
Open up the terminal and run the main.go program with the following command:
Congratulations! You just wrote and ran your first Go program! However, we do have a few more things to cover. -
Run the following line of code in your terminal:
Previously, runninggo run main.gosimultaneously compiled and executed your program in 1 step. This is nice for testing, but doesn't leave behind a compiled binary file that you can distribute and run on any compatible system that doesn't have Go downloaded. To do that, we need the build command. The above line of code (build) will generate your main.go program into an executable file namedhello.
Now you have an executable file of your first Go program!