Getting Started with Golang: Your First Steps
Hey there, aspiring Go programmer! If you're ready to dive into the world of Golang but aren’t sure where to start, you’re in the right place. This blog will guide you through the essentials: installing Go, setting up your environment, and picking an IDE or text editor. Let’s get your Go journey off to a smooth start!
Installing Go: A Step-by-Step Guide
Before you can start writing Go code, you need to install it on your machine. Don’t worry—it’s a straightforward process, and I’m here to walk you through it for Windows, macOS, and Linux.
1. Installing Go on Windows
Download the Installer:
- Go to the official Go downloads page and download the Windows installer (
.msi
file).
- Go to the official Go downloads page and download the Windows installer (
Run the Installer:
- Double-click the downloaded file to start the installation. Follow the prompts, and make sure to keep the default settings unless you have a specific reason to change them.
Verify Installation:
- Open Command Prompt and type
go version
. If everything is set up correctly, you should see the Go version you installed.
- Open Command Prompt and type
2. Installing Go on macOS
Download the Installer:
- Visit the Go downloads page and download the macOS package (
.pkg
file).
- Visit the Go downloads page and download the macOS package (
Run the Installer:
- Double-click the
.pkg
file to begin the installation. Follow the instructions, and the Go binaries will be installed in/usr/local/go
.
- Double-click the
Verify Installation:
- Open Terminal and type
go version
. You should see the Go version number displayed.
- Open Terminal and type
3. Installing Go on Linux
Download the Tarball:
- Go to the Go downloads page and download the appropriate tarball for Linux (
.tar.gz
file).
- Go to the Go downloads page and download the appropriate tarball for Linux (
Extract the Tarball:
Open Terminal and navigate to the directory where you downloaded the file. Run:
tar -C /usr/local -xzf go1.xx.linux-amd64.tar.gz
Replace
go1.xx.linux-amd64.tar.gz
with the filename you downloaded.
Set Up Your Path:
Add Go to your PATH by editing the
.bashrc
or.zshrc
file in your home directory:export PATH=$PATH:/usr/local/go/bin
Reload your shell configuration with
source ~/.bashrc
orsource ~/.zshrc
.
Verify Installation:
- In Terminal, type
go version
to check that Go is correctly installed.
- In Terminal, type
Setting Up GOPATH and GOROOT Environment Variables
With Go installed, you need to set up a couple of environment variables: GOPATH
and GOROOT
.
GOROOT
: This is where Go is installed. Usually, you don’t need to set this manually if you use the default installation paths, but if needed, it should point to the Go installation directory (e.g.,/usr/local/go
).GOPATH
: This is where your Go workspace resides. It contains your Go projects and third-party packages. By default, this is set to your home directory’sgo
folder (e.g.,~/go
).
To set these variables:
Windows:
Open System Properties > Advanced > Environment Variables.
Add a new system variable named
GOPATH
and set it to your workspace directory (e.g.,C:\Users\YourName\go
).If necessary, add
GOROOT
pointing to your Go installation directory.
macOS/Linux:
Edit your shell configuration file (
.bashrc
,.zshrc
, etc.) and add:export GOROOT=/usr/local/go export GOPATH=$HOME/go export PATH=$PATH:$GOROOT/bin
Apply the changes with
source ~/.bashrc
orsource ~/.zshrc
.
Installing a Go IDE or Text Editor
A good IDE or text editor can make coding in Go a breeze. Here are two popular choices:
1. Visual Studio Code (VSCode)
VSCode is a versatile editor with robust support for Go. To set it up:
Install VSCode:
- Download and install VSCode from its official site.
Install Go Extension:
- Open VSCode, go to the Extensions view (
Ctrl+Shift+X
), and search for “Go”. Install the official Go extension by the Go team.
- Open VSCode, go to the Extensions view (
Configure Go:
- Open a Go file, and the extension will prompt you to install additional Go tools. Follow the prompts to complete the setup.
2. GoLand
GoLand is a powerful IDE specifically designed for Go development by JetBrains. It’s a great choice if you’re looking for advanced features and support.
Install GoLand:
- Download GoLand from the JetBrains website.
Set Up GoLand:
- Follow the installation instructions and configure GoLand to use your Go SDK. You might need to specify your
GOROOT
andGOPATH
during the setup process.
- Follow the installation instructions and configure GoLand to use your Go SDK. You might need to specify your
Wrapping It Up
And there you have it—your comprehensive guide to getting started with Golang! From installing Go on various platforms to setting up your environment and choosing the right tools, you’re all set to begin your Go programming adventure. So grab your favorite beverage, fire up your new Go environment, and start coding. Welcome to the world of Go, where efficiency and simplicity reign supreme! 🚀
Ready to dive in? The Go community is waiting to welcome you with open arms. Happy coding 🐳
Subscribe to my newsletter
Read articles from Shivam Jha directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Shivam Jha
Shivam Jha
LFX'24 @Kyverno | Web Dev | DevOps | OpenSource | Exploring Cloud Native Technologies.