Setup MacOs for Go Development
Monday in France Installing Go on MacOS.
Are you ready to dive into the world of Go? With its unique combination of speed, reliability, and simplicity, Go is quickly becoming one of the most popular programming languages out there. In this article, we’ll walk you through the process of installing Go on your MacBook, and show you how to set up a simple example project using Go modules. We’ll even give you some tips on how to configure Visual Studio Code for Go development, so you can start coding with confidence. So what are you waiting for? Let’s get started and explore the power of Go!
To follow along with this article, you need to have Homebrew installed. You can find the installation instructions on the official website.
A few years ago installing Go was a painful experience as you had to create a specific folder structure to make sure the dependency management would work correctly. I also remember having to fiddle around with things like
Nowadays all you need to do is execute the command:
At the time of writing, this command installs Go version
1.16. Afterward, you can verify the installation via
go version which should yield a response like:
With Go installed, it’s time to create a simple example project.
Create a new project
Let’s start by creating a directory for our project:
In Go, dependency management is done through the
go.mod file which can be created using the
go mod init command followed by the module’s name, typically the repository where the code is stored (e.g.
For now, we will just use
Once you run the
go mod init command in the example directory, a
go.mod file will be created that describes the module’s properties, including its dependencies on other modules.
To further develop the project, you can create a
main.go file in the same directory and include a “hello world” example.
To execute the code, we can use the go run command followed by the name of the Go file, like this:
This will compile and run the program, and you should see the output “Hello, Medium!” printed to the console.
Instead of using the default
fmt.Println() to print our message, we might want to use a real logger. One popular logging package for Go is
logrus. Since it’s not part of the standard library, we need to use Go modules to import it into our project.
To get started, we can update the code in main.go to:
To add the
logrus module to our project, we need to update our
go.mod file. We can use the
go mod tidy command to automatically add the module and update our dependencies. This command will also create a
go.sum file for module authentication purposes.
If you run the code again, it should display three log messages.
Visual Studio Code
When it comes to IDEs for Go development, two popular options are GoLand from JetBrains and Visual Studio Code from Microsoft. While GoLand offers more advanced features, it comes with a price tag, making it less suitable for hobby projects. Therefore, I chose to use Visual Studio Code for this project.
Homebrew makes installing Visual Studio Code easy:
To take full advantage of features like IntelliSense and code navigation in Visual Studio Code for Go development, you need to install the Go for Visual Studio Code extension.
To do this, open Visual Studio Code, navigate to
Code > Preferences > Extensions and search for the extension. Once installed, if any required command-line tools are missing, a pop-up will appear the first time you open a directory or workspace with Go code. Simply click “Install All” to install the missing tools.
The extension depends on some extra command-line tools. If they are missing you get to see a pop-up, the first time you open a directory or workspace which contains Go code. Click
With the default settings, Visual Studio Code requires a manual action to save the file you are working on. Unsaved changes have left me puzzled many times during local testing. I always enable auto-saving for Visual Studio Code. You can enable auto-saving via
File > Auto Save.
The installation process for Go and setting up a workspace has become much simpler compared to a few years ago, eliminating the need for complicated and error-prone folder structures.
In my experience with Go modules, I’ve found dependency management to be straightforward and reliable, giving me confidence in starting a new hobby project with the language.
The Go extension for Visual Studio Code is a great tool for Go development, providing features such as IntelliSense and code navigation. As a user of JetBrains IDEs for Java development, I may consider switching to GoLand for future Go programming ;).