Go is a relatively young programming language, launched in 2009, with simplicity at its heart. Whereas it might not have all the bells and whistles you might expect from a modern programming language (see “From .NET to GoLang: Where Did Everything Go?” and “From .NET to GoLang: Here We Go Again” for my critique), it has some features that make it unique, such as its first-class concurrency model using channels and goroutines, based on Tony Hoare’s 1978 Communicating Sequential Processes (CSP).
You can set up Go and write your first program easily in a couple of minutes, but in order to program comfortably, I recommend using an Integrated Development Environment (IDE) that you can use to debug your code. You can use whatever you like, but I’ll show you how to use Visual Studio Code (VS Code) to write and debug Go code.
Download and Install Go
First, you need to download and install Go. This link has all the instructions you need for Linux, Mac and Windows, and they’re pretty straightforward, so I won’t bother repeating all of them here.
If you’re on Linux, though, do note that the first command is actually two commands combined with an &&
operator, so you need to add sudo
before both of them. To make it super clear, you need to do the following steps on Linux:
- Download the latest Go package for Linux.
- Run the following to remove any previous Go installation (skip this if you’ve never installed Go before):
sudo rm -rf /usr/local/go
cd
into the directory where you downloaded the Go package.- Run the following to extract the Go package and put it in the right folder:
sudo tar -C /usr/local -xzf go1.20.3.linux-amd64.tar.gz
- Be sure to change the filename to the one you actually downloaded.
- Add the following to the end of ~/.profile:
export PATH=$PATH:/usr/local/go/bin
- Restart your computer for the change to take effect.
- Run
go version
to ensure that thego
command is now accessible in your terminal.
A Minimal Example
Create a new folder, and name it whatever you like (e.g. “hellogo”). In it, add a file called main.go, and add the following code in it:
package main
import "fmt"
func main() {
fmt.Println("Hello Go!")
}
Using Go from the Command Line
You can run the above program in your favourite terminal by cd’ing into the directory where you added main.go, and running the following command:
go run main.go
The output, as you would expect, is:
Hello Go!
If you want to compile the code into an executable, you can run the following command:
go build main.go
This generates an executable named main
. If you want to specify the name of the executable, you can add a -o
switch and provide the name you want. For instance, the following command generates an executable named myapp:
go build -o myapp main.go
It’s important to note that the file being compiled must be at the end of the command, so the -o
switch goes before it.
To run the executable:
- On Windows, run:
myapp
- On Linux or Mac, run:
./myapp
Setting up VS Code for Go
If you don’t have it already, download and install Visual Studio Code. Use it to open the folder where main.go is. At this point, we need to install 3 things:
In my experience, VS Code automatically prompts you to install these when you open a .go file in it, so the easiest way is to just accept. In case it doesn’t, you can install the Go extension yourself from the Extensions tab on the left side of VS Code, and you can install the other two by following their respective instructions.
Debugging Go
Once you’ve installed all three items from the previous section, VS Code becomes a handy tool to write, run and debug Go code with ease. In fact, you get things like syntax highlighting, code completion, and the ability to debug your code.
To run/debug your code directly from VS Code, you can press F5, or do it via the “Run and Debug” tab (which looks like a Play button with a beetle) on the left side of VS Code. Doing this immediately gives us an error:
That’s because we missed a step. In the bottom part of VS Code, switch to the integrated terminal (“Terminal” tab) or, if you don’t have one, open a new one via the Terminal menu at the top of VS Code. Then, run the following command:
go mod init main
This creates a go.mod file:
You can now press F5 again to debug Go. You can set a breakpoint by clicking next to a line number; this will show the breakpoint as a red circle, and the code will stop when it reaches that point, allowing you to step through the code, inspect variables, and see other parts of the state of the program:
There isn’t much to debug in this simple program, but at least this gives you what you need to be able to debug more interesting programs as you write them. Debugging is an entire topic in itself that’s out of scope for this article, but it’s an essential skill for a competent developer. You can learn more about it from:
- The Debugging doc at the VS Code Go extension’s GitHub repository – explains basic debugging operations.
- Go in Visual Studio Code – shows more advanced things you can do with the extension.
- Working with VS Code Launch Configurations – another article here at Gigi Labs showing how you can configure the way you run your programs in VS Code (e.g. passing command line parameters).
One other tip: while you are in a debugging session, VS Code creates a temporary executable called __debug_bin. If you’re using source control, be sure to exclude it (e.g. add it to your .gitignore file when using Git).
Conclusion
Go is fairly straightforward to set up and use:
- Download Go and follow the official instructions to set it up.
- Use Go from the command-line if you need to (e.g. for Continuous Integration).
- Set up the Go extension, Delve debugger and gopls language server in VS Code.
- Press F5 to debug your Go programs in VS Code!
That’s all you need to keep Going!