Install and configure Go
First click the download button on the official website to enter this interface, then find the version you want to download, right-click to copy the link
and then use wget to download the file, here is the above file as an example
wget -c https://golang.google.cn/dl/go1.20.3.linux-amd64.tar.gz
Unzip the file to the target /usr/local directory
tar -C /usr/local -xzf go1.20.3.linux-amd64.tar.gz
Then configure the environment variables, here take Ubuntu as an example, open the /etc/bash.bashrc file, and add at the end
# Go environment
export PATH=$PATH:/usr/local/go/bin
# 注意,下面的目录是自己设定的目录,我的用户名为mk所以前缀就设定为/home/mk/
export GOPATH=/home/mk/software/go
export GOBIN=$GOPATH/bin
Then use source to activate the configuration in the current shell
source /etc/bash.bashrc
Note that if your system does not have a /etc/bash.bashrc file, you can use the above steps for the /etc/.bashrc file or ~/.bashrc file
Frequently Asked Questions about Environment Variables
Q: sudo go
When using the command, it prompts that the go command cannot be found
A: This is because there is no sudo environment variable configured, and the /etc/sudoers file needs to be modified. Add field Q to the front of the secure_path
field : ordinary users can use Go commands normally, but when switching from ordinary users to Root users, using the Go command again prompts that Go is not installed A: This situation usually occurs when the environment variable is configured in ~/.bashrc or /etc/profile. A new shell process is created, which will not automatically execute the configuration in /etc/profile, and the ~/.bashrc files of ordinary users and root users are different . The solution is to configure environment variables in the /etc/bash.bashrc or /etc/.bashrc file. Or configure ~/.bashrc in normal users and Root users respectively/usr/local/go/bin:
Configure the Vscode plugin
After Go is installed, you can configure the Vscode plug-in.
First search and install the Go plug-in.
If you encounter an error during the installation process, such as a network error, you need to install it manually. First, run the
following code to proxy
go env -w GO111MODULE=on
go env -w GOPROXY=https://goproxy.io,direc
Take gopls as an example
go install -v golang.org/x/tools/gopls@latest