1. Download and Install Go

To start developing with Go language, the first step is to install Go in your local environment. Below are the steps to download and install Go from the official Go language website:

  1. Visit the official website of Go language at https://go.dev/dl/.
  2. Choose the installation package suitable for your operating system. Go language provides installation packages for Windows, macOS, and Linux systems. For example, if you are using Windows, you should choose the MSI file; for macOS, it's usually a .pkg file, and for Linux, it's a .tar.gz file.
  3. Once downloaded, double-click to run the installation file.
  4. Follow the installation wizard steps, choose the installation path, and accept the license agreement.
  5. After the installation is complete, restart your computer to ensure the configuration takes effect.

Here are some specific installation commands for different systems:

  • Windows: In the Windows system, you can use the official MSI file to complete the installation through the graphical user interface.

  • macOS: In macOS, there are usually two installation methods: using the .pkg graphical installer or using Homebrew through the command line. For example, the command to install Go using Homebrew is:

   brew install go
  • Linux: In the Linux system, you can download the .tar.gz file and then unzip it to /usr/local (or other custom directories). Here is a typical installation command example:
   wget https://golang.org/dl/go1.16.linux-amd64.tar.gz
   sudo tar -C /usr/local -xzf go1.16.linux-amd64.tar.gz

2. Configure Environment Variables

After installing Go, it's necessary to set up the environment variables correctly so that you can execute Go commands from any location and compile programs properly.

  • GOROOT: Points to the installation path of Go. If you installed Go using a package manager, you usually don't need to set this variable manually.
  • GOPATH: Before Go version 1.11, this pointed to your workspace directory. It was the place where you stored Go source code, compiled binary files, and dependencies. After Go 1.11, Modules were introduced, and the role of GOPATH has diminished.
  • GOBIN: Points to the directory where the compiled binary files are stored. This is an optional setting, and if not set, the default GOPATH/bin directory will be used.

Configuration for Windows System

  1. Right-click "My Computer" or "This PC" and select "Properties".
  2. Go to "Advanced system settings" and click "Environment Variables".
  3. Under "System variables", click "New" to add GOROOT pointing to the Go installation directory, for example, C:\Go.
  4. Similarly, add the GOPATH variable and point it to your workspace, for example, C:\Users\name\go.
  5. (Optional) Set GOBIN if you want the compiled files to be stored in a specific directory.

Configuration for macOS

In macOS, you can typically edit the .bash_profile, .bashrc, .zshrc, etc., files in the terminal to add environment variables (depending on the shell you are using).

export GOROOT=/usr/local/go
export GOPATH=$HOME/go
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin

Linux System Configuration

Similar to macOS, you can add the above environment variables in the .bashrc or .profile file in the user directory.

export GOROOT=/usr/local/go
export GOPATH=$HOME/go
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin

Remember to use the command source ~/.bashrc (or the corresponding shell configuration file) to activate the changes immediately after completing the configuration.

3. Choice of Development Tools

Go language is a flexible programming language. You can use various IDEs or text editors to write Go code. Here are a few common IDEs and editors:

  • Visual Studio Code
  • IntelliJ IDEA with Go plugin
  • GoLand
  • Atom with Go plugin
  • Sublime Text with Go plugin

For beginners, I recommend using Visual Studio Code (VS Code). It is a free and open-source editor with good support for the Go language and an active community.

Advantages of VS Code include:

  • Built-in git operations.
  • Extensive plugin ecosystem, including excellent extensions for the Go language, such as the Go plugin.
  • Cross-platform support, running on Windows, macOS, and Linux systems.

To start writing Go code in VS Code, you can install the Go plugin, which provides features like autocompletion, go to definition, code snippets, and many more. Simply access the VS Code Extension Marketplace, search for "Go", and install it.