Programming

【Go Language】Environment Setup Using goenv

Overview

This article is intended as a first step for those who want to start learning the Go programming language.
As a beginner programmer, I wanted to document the process of setting up the development environment—often the first stumbling block—for my own reference as well.
Referring to the TIOBE Index for popular programming languages, Go has steadily risen in popularity, moving from 13th place in 2023 to 7th place as of November 2024. It might be a good time to start learning it!

TIOBE Programming Language Popularity Rankings (January 2024)

By the way, since I’m a Mac user, this guide focuses on setting up the environment on macOS.
(For Windows users, my apologies 🙇. However, you might still find some parts helpful!)

Installing Homebrew

Let’s start by installing Homebrew, the package manager for macOS. If you’ve already installed it, feel free to skip this step!
First, for those wondering what a package manager is, here’s a simple explanation with the help of the diagram below!

If you want to use Git on your PC, most of you will likely benefit from using a package manager. One of the advantages of using a package manager is that it automatically fetches all the necessary packages required to run Git along with Git itself.

If you don't use a package manager, you would first need to download the Git package and then manually check and fetch each required package yourself. (This can be quite a hassle…)

Now that you have a general understanding of the importance of package managers, let’s install Homebrew!
Run the following command:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Once the installation is complete, you'll likely see a message instructing you to run a specific command. Go ahead and run it! (Think of it as a little charm to make Homebrew usable on your PC.)

echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zprofile
eval "$(/opt/homebrew/bin/brew shellenv)"

Finally, run the command below. If it displays something like /opt/homebrew/bin/brew, the installation was successful.

which brew

Installing goenv

Next, we’ll start by installing goenv, a version management tool for the Go programming language.
With this tool, you can easily switch between different versions of Go as needed.

brew install goenv

Once the installation is complete, make sure to add goenv to your PATH!

echo 'export GOENV_ROOT="$HOME/.goenv"' >> ~/.zshrc
echo 'export PATH="$GOENV_ROOT/bin:$PATH"' >> ~/.zshrc
echo 'eval "$(goenv init -)"' >> ~/.zshrc
source ~/.zshrc 

Now, let’s install the latest version of Go. You can check the available versions using the following command:

goenv install -l

You’ll see a list of Go versions displayed like this:
Note: If the latest version of Go is not listed, run brew upgrade goenv to fetch the latest version.

Currently, the latest version appears to be 1.23.3. Download and set your PC to use version 1.23.3 with the following command:

goenv install 1.23.3
goenv global 1.23.3

Use the command below to confirm that the currently active version of Go is set to 1.23.3. If it is, the setup is complete.

go version

Setting Up VSCode

Next, let’s configure VSCode to make Go development easier!
Create a .vscode folder in the directory you’ve opened in VSCode, and inside it, create a settings.json file.
Once you’ve created settings.json, paste the following configuration into the file:

{
  "go.formatTool": "goimports",
  "go.lintTool": "golangci-lint",
  "go.lintFlags": ["--fast"],
  "go.goroot": "<goルートのパスを貼り付け>",
  "go.useLanguageServer": true,
  "editor.formatOnSave": true,
  "[go]": {
    "editor.defaultFormatter": "golang.go"
  }
}

For the go.goroot setting, paste the path to the Go root directory displayed by the following command:
Note: Every time you switch to a different Go version, the Go root path will change. Don’t forget to update the go.goroot setting in settings.json accordingly!

go env GOROOT

Finally, install the following Go language plugin, and your environment setup will be complete! Great job!
With this setup, you should now be able to develop in Go smoothly using VSCode. 🎉

Let’s try running some Go code!

First, create a file named main.go and actually run it. Place main.go in the same directory level as the .vscode folder.

Next, copy and paste the following code into main.go:

package main

import (
	"fmt"
)

func main() {
	fmt.Println("hello world")
}

Now you’re all set! Run the following command in the same directory.
Once executed, you should see hello world displayed in the terminal!

go build main.go
./main

If you check the folder contents, you’ll notice a new file named main has been created.
This file is generated by the go build command, which compiles main.go. The ./main command is used to execute the generated file.

In Go, the standard workflow involves building the file first and then running the generated file, so make sure to remember this process!

Summary

In this tutorial, we shared how to set up your Go environment using goenv.
With this, you now have a solid foundation to start learning Go!

Happy coding!

Sponsored Link

  • Author

kaz

Full-stack Engineer specializing in Backend/Frontend/Cloud Infrastructure | Digital Nomad since June 2023, traveling the world | Sharing programming tips and insights | Posting travel updates on X

-Programming
-,