Skip to main content

Environment Setup

Preface

This article will guide you through installing and configuring the Go language environment.

Step 1: Install Go Language

1. Visit the Go Official Website

First, open the Go official website, where you can find various versions of Go. Choose the version suitable for your operating system.

2. Download and Install

Based on your operating system (Windows, macOS, or Linux), select the corresponding installation package:

  • Windows: Download the .msi installer and double-click to run it, following the prompts to complete the installation.
  • macOS: Download the .pkg installer, open it, and follow the prompts to complete the installation.
  • Linux: You can use a package manager or download the .tar.gz file and extract it for installation.

3. Configure Environment Variables

After installation, ensure that Go's binary directory is added to your system's PATH environment variable.

  • Windows: The installer typically adds Go to PATH automatically. If not, you can manually add Go’s installation path (e.g., C:\Go\bin).

  • macOS 和 Linux: Open a terminal and edit your shell configuration file (such as .bash_profile or .zshrc), adding the following line:

    export PATH=$PATH:/usr/local/go/bin

    Then, run source ~/.bash_profile or source ~/.zshrc to apply the changes.

4. Verify Installation

Enter the following command in your terminal to verify that Go was installed successfully:

go version

If you see an output like go version go1.20.0 darwin/amd64, Go has been successfully installed.

Set Up Go Workspace

Go has a unique concept of a workspace, where it stores your Go code, compiled packages, and executables.

1. Set GOPATH

The workspace is defined by the GOPATH environment variable. You can create a folder anywhere on your system to serve as your workspace. For example, create a go folder in your home directory:

mkdir $HOME/go

Then, set the GOPATH environment variable to point to this directory:

export GOPATH=$HOME/go

As with the PATH variable, add this configuration to your shell profile and apply the changes.

2. Workspace Structure

A Go workspace usually contains three folders:

  • src: Stores the source code.
  • pkg: Stores the compiled package files.
  • bin: Stores the compiled executable files.

Within the src folder, you can create subfolders based on your project or package names and start writing Go code there.

Choose Development Tools

Selecting the right development tools can significantly improve productivity. Below are some popular tools for Go development:

  • Visual Studio Code: A free code editor with a robust plugin system. You can install the Go plugin for code completion, debugging, and formatting.
  • GoLand: A professional Go IDE developed by JetBrains, packed with powerful features, but requires a paid license.
  • Sublime Text: A lightweight text editor that can be extended with plugins to support Go development.

Once your development tool is installed and configured, you're ready to start writing Go code.

Conclusion

By completing these steps, you have successfully set up the Go development environment. You can now create and run your first Go program and begin your journey in learning Golang. Remember, tools and environment setup are just the starting point. Continuous learning and hands-on practice are key to mastering the language.


Copyright Notice: Free to Share - Non-commercial - No Derivatives - Keep Author - Keep Source

Author: afxcn

Source: https://gostartkit.com/docs/golang/environment-setup

Date: September 4, 2024