How to setup local development environment and run your first Go program.
As someone who is taking baby steps into programming with Go, you may be uncertain on how to get started. Purpose of this article is to provide a helping hand in those initial steps. We’ll discuss about how to install Go’s binary distribution, setup local development environment and write a basic program. We’ll also look into how you can perform basic debugging activities along with building a binary and executing the same. Without much ado, let’s get started.
This article is intended for those who are relatively new to Go programming language, but, may have some experience with other programming languages. If you are already familiar with Go, kudos! You could use this article as a refresher. :-)
The Go team provides binaries for various Operating Systems such as Windows, Linux and Mac OS. Download a binary distribution relevant to your Operating System and follow installation instructions from here.
Once installation is complete, run the following command to ensure that Go is correctly installed.
If the installation is successful, you can see the Go version details, similar to the following:
Code Editor / IDE
Now that Go has been installed successfully, let’s try to identify a good code editor or IDE to use.
Though there are multiple editors available at this point, I would suggest to use any of the two options mentioned below. Please note that, this is my personal preference. If you’ve any other choice, please feel free to proceed.
Visual Studio Code
Visual Studio Code (VS Code) is one of the most popular free code editors available these days. It’s Go extension provides many features, such as intellisense, code navigation among a few, which are helpful to the developers. This extension was initially created by VS Code team. Later, it was transferred to Go team.
Download VS Code from here. After installation, traverse to VS Code extensions sections, and search for Go. You can see the following Go extension. Install the same.
Post installation of the Go extension, VS Code is ready for coding.
Though VS Code provides a decent coding experience, sometimes you may need a bit more advanced features; such as refactoring, better imports, advanced debugging etc. In such cases, you may choose JetBrains GoLand. Note that, GoLand is a paid IDE. It provides free 30 day trial, if you prefer to explore. The installer can be obtained from here.
In this article, we’ll use VS Code with Go extension.
First, let’s create a new directory named hello-go to keep our Go code. Traverse to the newly created directory and run the following command:
go mod init myname.com/hello-go
(Feel free to replace myname.com with something else; e.g. your GitHub handle)
On a Linux machine, following are the commands you would execute to perform above steps:
Once above commands are executed, a new directory and a new file, go.mod, will be created. The go.mod would look similar to the following:
With the project directory and go.mod file created now, we are all set to proceed with writing our first Go program. But, before doing so, let’s try to understand why we executed the go mod init command and it’s implications.
Prior to Go v1.11, developers had to configure an environment variable called GOPATH while setting up their Go workspace. Value of this variable acted as the root of the Go workspace. The GOPATH approach had many deficiencies which forced developers to confine themselves to a single workspace. Due to these challenges and as an attempt to improve dependency management, an alternate concept called modules were introduced in v1.11. With modules, configuring GOPATH is no longer needed. When the module feature is enabled, details about the project (or module) and it’s dependencies are stored in a file called go.mod. This file also contains minimum Go version the module needs. Starting v1.16, module feature is enabled by default for all Go build operations.
When we run the go mod init command, it creates a new module and initializes go.mod file with relevant information. More details on go mod command can be found here.
A Go program consists of one or more packages. A package may contain one or more source files (i.e.: files with .go extension). By convention, all files belonging to a single package are located in a single directory.
A simple Go source file consists of three main parts:
- package statement
- import statement(s)
- Source code
For a Go project/program, we need to create at least one Go file (with .go extension). If there is only one Go file in the project, it needs to follow two rules:
- It should be part of main package and
- It should have a function called main.
When the program is executed,compiler invokes main function in the main package. (Note that, if our intention is to create a library, these two rules are not applicable.)
Let’s open the project directory in VS Code, create a file with the following content and name it as main.go.
The project structure would look something similar to the following:
Once the main.go file is created, let’s try to execute the program. To do so, open a terminal in VS Code by clicking on Terminal -> New Terminal. A new terminal window would open within the editor. Run the following command to execute the program:
go run .
You would be able to see something similar:
As a developer, you may need to debug the program at times. In order to enable debug, you need to install another component called Delve. You can find more details on Delve here.
For now, execute the following command in the VS Code terminal:
$ go install github.com/go-delve/delve/cmd/dlv@latest
Once the command completes execution, restart VS Code, open the project directory again.
To start debugging, follow these steps:
- Open main.go file in VS Code.
- Add a breakpoint at the fmt.Println() statement.
- Right click on main.go file and select Command Palette option.
- Search for Debug: Start Debugging
- If prompted, select Go
You should be able to see something similar to the following:
The program has started execution and is waiting at the breakpoint. It’ll wait till next action is performed. Click on Continue button to proceed.
Add External Dependencies
The program we wrote above is a very simple program, which uses an in-built package. Now, let’s modify main.go program to use an external library. Let’s use a logging library named logrus for this purpose.
To install logrus in your machine, execute the following on the VS Code terminal:
$ go get "github.com/sirupsen/logrus"
You would be able to see the progress as follows:
The modified go.mod would like like following:
As you can see, the dependency details for logrus has been added to the go.mod file.
Now, let’s add a log statement in the code by replacing fmt package with logrus.
The new code looks like the following:
Here, we imported the logrus package and gave an alias as log. Then, using the alias, we invoked the Println() function.
When you run the program, you would get an output similar to the following:
Now, it’s time to create an executable, so that it can be distributed to others.
On Linux, execute the following on the VS Code Terminal:
$ go build -o build/hello-go
This will create an executable called hello-go in build sub-directory.
On Windows, execute the following on the VS Code Terminal:
$ go build -o build/hello-go.exe
This will generate an executable called hello-go.exe in build sub-directory.
In case you would like to generate binaries for multiple platforms & architectures, you could do as follows (On Linux, Windows or Mac).
For Linux 64 bit target system:
$ env GOOS=linux GOARCH=amd64 go build -o build/hello-go
For Windows 64 bit target system:
$ env GOOS=windows GOARCH=amd64 go build -o build/hello-go.exe
Now that the binary has been created, let’s execute it as follows:
The result would be something similar to the following:
Voila! We’ve created an executable binary from a program written in Go!
In this article, we went through how to install Go, setup local environment, debug, run and build a binary.
Thank you for reading so far. Till we meet next time, happy coding!