02 Environment Preparation How to Install and Configure a Basic Go Development Environment

02 Environment Preparation How to Install and Configure a Basic Go Development Environment #

Hello, I’m Kong Lingfei.

In the previous lecture, we talked about the functions and architecture of the IAM system. In the next two lectures, we will deploy it on your server. However, before the formal deployment, we need to prepare a Go development environment, because we obtain the required binary files for deployment by compiling the source code.

Therefore, in today’s lecture, I will first guide you step by step to configure a Go development environment for your future development and compilation. In the next lecture, I will guide you in deploying the IAM system.

To configure a Go development environment, we can follow these 4 steps:

  1. Application and configuration of a Linux server
  2. Installation and configuration of dependencies
  3. Installation and configuration of the Go compilation environment
  4. Installation and configuration of a Go development IDE

Applying for and Configuring a Linux Server #

Without a doubt, in order to install a Go development environment, you first need to have a Linux server. There are many operating systems available for Linux servers, such as CentOS, Ubuntu, RHEL, Debian, etc. However, the most commonly used in production environments is still the CentOS system. To stay consistent with the production environment, we will choose the latest version of CentOS at the moment: CentOS 8.2.

Since all the operations in this column are carried out on CentOS 8.2, to avoid operations failures due to inconsistent environments, I suggest that you also use CentOS 8.2. Installing a Linux server requires two steps: applying for the server and configuring it.

Applying for a Linux Server #

We can install a CentOS 8.2 system in three ways:

  1. Install a CentOS 8.2 system on a physical machine.
  2. Install virtual machine management software on Windows/MacBook and create a CentOS 8.2 virtual machine using the virtual machine management software. For Windows, it is recommended to use VMWare Workstation to create the virtual machine, and for MacBook, it is recommended to use VirtualBox.
  3. Purchase a virtual machine on platforms such as Tencent Cloud, Alibaba Cloud, Huawei Cloud, etc., and pre-install the CentOS 8.2 system.

Configuring the Linux Server #

After applying for a Linux server, we need to log in to the Linux server using tools such as SecureCRT or Xshell, and perform some necessary configurations on the server, including creating a regular user, adding sudoers, and configuring the $HOME/.bashrc file. Let’s go through them step by step.

Step 1: Log in to the Linux system with the root user and create a regular user.

In general, a project is completed by multiple developers. In order to save costs, companies will not provide a separate server for each developer, but rather allow all developers to share a development machine and carry out development by logging in to the development machine with a regular user. Therefore, to simulate a real enterprise development environment, we will also use a regular user’s identity to carry out project development. Here is how you can create a regular user:

# useradd going # Create the "going" user to log in to the development machine for development
# passwd going # Set a password
Changing password for user going.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.

Not only that, logging in to and operating the development machine with a regular user can also ensure the security of the system. This is a good practice, so we should also try to avoid using the root user in our daily development.

Step 2: Add sudoers.

We know that many times, regular users also need some of the privileges of the root user. But the password of the root user is usually maintained and regularly changed by the system administrator, so it is inconvenient to ask the administrator for the password every time. Therefore, I suggest that you add the regular user to the sudoers, so that the regular user can temporarily obtain the root privileges using the sudo command. You can execute the following command to add it:

# sed -i '/^root.*ALL=(ALL).*ALL/a\going\tALL=(ALL) \tALL' /etc/sudoers

Step 3: Replace the default Yum source in CentOS 8.4.

Since Red Hat has announced that support for CentOS 8 will be discontinued on December 31, 2021, the official Yum source is no longer available. Therefore, you need to switch to a different Yum source, and here we will choose the Yum source provided by Alibaba. The command to switch is as follows:

# mv /etc/yum.repos.d /etc/yum.repos.d.bak # Backup the original Yum source first
# mkdir /etc/yum.repos.d
# wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
# yum clean all && yum makecache

Step 4: Log in to the Linux server with the new username (going) and password. This step can also verify if the regular user was created successfully.

Step 5: Configure the $HOME/.bashrc file.

The first step after logging in to the new server is to configure the $HOME/.bashrc file to make the Linux login shell more user-friendly. For example, configuring LANG can solve the problem of garbled characters, and configuring PS1 can avoid displaying the entire path, and adding \\)HOME/bin to the PATH variable. The configuration content is as follows:

# .bashrc

# User specific aliases and functions

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi

# User specific environment
# Basic envs
export LANG="en_US.UTF-8" # Set the system language to en_US.UTF-8 to avoid garbled characters in the terminal
export PS1='[\u@dev \W]\$ ' # The default PS1 setting shows the full path, to prevent it from becoming too long, here we only display: "username@dev last directory name"
export WORKSPACE="$HOME/workspace" # Set the working directory
export PATH=$HOME/bin:$PATH # Add the $HOME/bin directory to the PATH variable

# Default entry folder
cd $WORKSPACE # After logging in to the system, enter the workspace directory by default

One thing to note is that when exporting PATH, it is better to put $PATH at the end, because we expect the commands added to the directory to be searched and used first. After configuring the $HOME/.bashrc file, we also need to create the workspace directory. By putting all work files in the $HOME/workspace directory, we can enjoy the following benefits:

  • It keeps our $HOME directory tidy, making it easier to find and categorize files in the future.
  • If one day the / partition runs out of space, we can move the entire workspace directory to another partition and keep a soft link in the / partition, for example: /home/going/workspace -> /data/workspace/.
  • If we want to back up all work files one day, we can directly back up the workspace directory.

The specific command to create the directory is $ mkdir -p $HOME/workspace. After configuring the $HOME/.bashrc file, we can execute the bash command to load the configuration into the current shell.

With that, we have completed the application and initial configuration of the Linux development environment.

Dependency Installation and Configuration #

Installing the IAM system on a Linux system requires some RPM packages and tools, some of which are direct dependencies and some are indirect dependencies. To avoid dependency errors in subsequent operations, such as compilation or command execution errors due to missing packages, we need to install and configure the dependencies uniformly. The installation and configuration steps are as follows.

Step 1: Install dependencies.

First, on a CentOS system, we use the yum command to install the dependencies of the required tools. The installation command is as follows:

$ sudo yum -y install make autoconf automake cmake perl-CPAN libcurl-devel libtool gcc gcc-c++ glibc-headers zlib-devel git-lfs telnet lrzsz jq expat-devel openssl-devel

Although some CentOS 8.2 systems already have these dependencies installed by default, I will still attempt to install them to ensure that they are all installed. If the system prompts Package xxx is already installed., it means that it is already installed, and you can ignore it.

Step 2: Install Git.

Since installing the IAM system, running the go get command, and installing the protobuf tool are all done through Git, we need to install Git next. Since older versions of Git do not support the --unshallow parameter, and the go get command uses the git fetch --unshallow command when installing Go packages, we need to make sure to install a newer version of Git. The specific installation method is as follows:

$ cd /tmp
$ wget --no-check-certificate https://mirrors.edge.kernel.org/pub/software/scm/git/git-2.36.1.tar.gz
$ tar -xvzf git-2.36.1.tar.gz
$ cd git-2.36.1/
$ ./configure
$ make
$ sudo make install
$ git --version          # Output the Git version to confirm a successful installation
git version 2.36.1

Note that after installing Git using the above steps, we need to add the Git binary directory to the PATH environment variable, otherwise Git may report errors due to not finding some commands. You can use the following command to add the directory:

tee -a $HOME/.bashrc <<'EOF'
# Configure for git
export PATH=/usr/local/libexec/git-core:$PATH
EOF

Step 3: Configure Git. We can directly execute the following commands to configure Git:

$ git config --global user.name "Your Name"    # Replace with your own username
$ git config --global user.email "[your email address]"    # Replace with your own email address
$ git config --global credential.helper store    # Set Git to store usernames and passwords
$ git config --global core.longpaths true # Solve the 'Filename too long' error in Git

In addition to configuring Git as outlined above, we also need to pay attention to a few points.

First, in Git, non-ASCII characters are referred to as Unusual characters. By default, these characters are output to the terminal using octal escape characters (to prevent garbled output), but most terminals nowadays support directly displaying non-ASCII characters. Therefore, we can turn off this feature, the specific command is as follows:

$ git config --global core.quotepath off

Second, GitHub limits cloning a maximum of 100MB for a single file. To be able to clone files larger than 100MB, we also need to install Git Large File Storage. The installation method is as follows:

$ git lfs install --skip-repo

Great! Now we have completed the installation and configuration of the dependencies.

Installation and Configuration of Go Compilation Environment #

As we know, Go is a compiled language, so before deploying the IAM system, we need to compile the code into executable binary files. Therefore, we need to install the Go compilation environment.

In addition to Go, we will also use the gRPC framework to demonstrate the usage of the RPC communication protocol, so we also need to compile the .proto files of ProtoBuf into Go language interfaces. Therefore, we also need to install the ProtoBuf compilation environment.

Installation and Configuration of Go Compilation Environment #

Installing the Go language is relatively simple. We just need to download the source code package and set the corresponding environment variables.

First, let’s download the corresponding Go installation package and source code package from the official Go website. Here, I downloaded version go1.18.3:

$ wget -P /tmp/ https://golang.google.cn/dl/go1.18.3.linux-amd64.tar.gz

Next, let’s proceed with the unpacking and installation using the following commands:

$ mkdir -p $HOME/go
$ tar -xvzf /tmp/go1.18.3.linux-amd64.tar.gz -C $HOME/go
$ mv $HOME/go/go $HOME/go/go1.18.3

Then, let’s execute the following command to append the following environment variables to the $HOME/.bashrc file:

$ tee -a $HOME/.bashrc <<'EOF'
# Go envs
export GOVERSION=go1.18.3 # Set the Go version
export GO_INSTALL_DIR=$HOME/go # Set the Go installation directory
export GOROOT=$GO_INSTALL_DIR/$GOVERSION # Set GOROOT
export GOPATH=$WORKSPACE/golang # Set GOPATH
export PATH=$GOROOT/bin:$GOPATH/bin:$PATH # Add the Go language's built-in and go install installed binaries to the PATH
export GO111MODULE="on" # Enable Go modules feature
export GOPROXY=https://goproxy.cn,direct # Set the proxy server for installing Go modules
export GOPRIVATE=
export GOSUMDB=off # Disable checksum verification for Go dependencies
EOF

Why do we need to add so many environment variables? This is because Go uses a series of environment variables to control the behavior of the Go compiler. Therefore, it is important to understand the meaning of each environment variable.

Since Go will use Go modules to manage dependencies in the future, I recommend setting GO111MODULE to on.

When using modules, $GOPATH is meaningless. However, it will still store downloaded dependencies in the $GOPATH/pkg/mod directory and put binary files installed by go install in the $GOPATH/bin directory.

In addition, we also need to add $GOPATH/bin and $GOROOT/bin to the Linux executable file search PATH. This way, we can directly execute the commands provided by Go in the bash shell, as well as the commands installed through go install.

Next, let’s test it. If we can successfully output the Go version by executing the go version command, it means that the Go compilation environment is installed successfully. The specific command is as follows:

$ bash
$ go version
go version go1.18.3 linux/amd64

Finally, let’s initialize the workspace.

The Go version used in this column is go1.18.3, which supports multi-module workspaces. Therefore, here we also need to initialize the workspace. The initialization command is as follows:

$ mkdir -p $GOPATH && cd $GOPATH
$ go work init
$ go env GOWORK # Execute this command to view the path of the go.work workspace file
/home/going/workspace/golang/go.work

Installation of ProtoBuf Compilation Environment #

Next, let’s install the protoc compiler for protobuf. protoc relies on protoc-gen-go to convert code in Go language, so we need to install both protoc and protoc-gen-go. The installation method is relatively simple. Just follow the code and operation comments I provided below.

# Step 1: Install protobuf
$ cd /tmp/
$ git clone -b v3.21.1 --depth=1 https://github.com/protocolbuffers/protobuf
$ cd protobuf
$ ./autogen.sh
$ ./configure
$ make
$ sudo make install
$ protoc --version # Check the protoc version by outputting the version number, indicating a successful installation
libprotoc 3.21.1

# Step 2: Install protoc-gen-go
$ go install google.golang.org/protobuf/cmd/protoc-gen-go

When you execute the go install command for the first time, because there is no local cache, it needs to download all the dependent modules. Therefore, the installation speed may be slow, so please be patient.

Installation and Configuration of Go Development IDE #

After setting up the development environment, you will need a code editor to start developing Go projects. In order to improve productivity, you also need to configure this editor as a Go IDE.

Currently, IDEs like GoLand and VSCode are excellent choices for Windows systems. However, on Linux systems, you can choose to configure Vim as a Go IDE. Once you are familiar with Vim IDE operations, it can be just as efficient as GoLand and VSCode. There are multiple ways to configure Vim as an IDE, but here I will choose to use vim-go, a popular Vim Go development plugin, to configure Vim as a Go IDE.

The installation and configuration of Vim IDE can be done in the following 2 steps.

Step 1: Install vim-go.

The installation command is as follows:

$ rm -f $HOME/.vim; mkdir -p ~/.vim/pack/plugins/start/
$ git clone --depth=1 https://github.com/fatih/vim-go.git ~/.vim/pack/plugins/start/vim-go

Step 2: Install Go tools.

vim-go relies on some Go tools, such as guru and godef for function jumping, and goimports for formatting. Therefore, you also need to install these tools. The installation can be done by executing vi /tmp/test.go, and then entering :GoInstallBinaries to install the required tools for vim-go.

The commonly used key mappings for Go IDE operations are shown in the following table:

Key Mappings

Summary #

In this lesson, we installed and configured a Go development environment together. To facilitate your review, I have created a flowchart illustrating the installation and configuration process, as shown below.

With this development environment, we can now code anytime during the learning process to familiarize ourselves with and validate the concepts. Therefore, it is important that you complete the deployment of this lesson before proceeding to the next lessons.

Exercise after class #

  1. Try to write a main.go file that prints Hello World in the main function, then execute go run main.go to test the Go development environment.
  2. Try to write a main.go file with the following code:
package main

import "fmt"

func main() {
    fmt.Println("Hello World")
}

Place the cursor on Println, press Enter to jump to the function definition, and press Ctrl + I to return to the jump point.

I look forward to seeing your thoughts and answers in the comments section. Feel free to discuss any issues you encountered during the installation process. See you in the next lesson!