Go Environment Variables
For convenience in development, three environment variables often need to be set in the development environment:
$GOROOT
: The installation directory ofgo
, which remains unchanged after configuration;$GOPATH
: The project root path of thego
project in the local development environment (for project compilation,go build
,go install
), this environment variable can differ for different projects during compilation;$PATH
(important): Thebin
directory ofgo
needs to be added to the system$PATH
to conveniently use related go commands, and it also remains unchanged after configuration;
There is also a detailed explanation of Go's environment variables in the official documentation, please refer to the link: https://golang.google.cn/doc/install/source
The
$GOOS
and$GOARCH
environment variables are two very practical variables that can be used in cross-compilation for different platforms. You only need to set these two variables beforego build
, which is one of the advantages of the go language: it can compile and generate executable files that run across platforms. It feels more efficient and lightweight compared to QT, although the generated executable files are a bit larger, they are still within an acceptable range. For example, to compile an executable file forWindows x86
under theLinux amd64
architecture, you can use the following command:CGO_ENABLED=0 GOOS=windows GOARCH=386 go build hello.go
Unfortunately, cross-compilation temporarily does not support the
cgo
method, so you need to set the environment variable$CGO_ENABLED
to 0. This will generate ahello.exe
executable file for thewindows x86
architecture in the current directory after execution.
Environment Variable Settings
Other than the $PATH
environment, other environment variables are optional.
Why is this step optional? Because future versions of Go
are gradually beginning to remove support for $GOPATH
/$GOROOT
. In addition, there is a Terminal
function integrated into the Goland
IDE where the environment variables are already set.
Setting Environment Variables on *nix
On *nix
systems (such as Linux/Unix/MacOS/*BSD
, etc.), you need to add the following environment variable settings to /etc/profile
and then execute the command #source /etc/profile
to reload the profile configuration file (or log in again) to add the following variables to the user's environment variables:
export GOROOT=/usr/local/go
export GOPATH=/Users/john/Workspace/Go/GOPATH
export PATH=$GOPATH/bin:$GOROOT/bin:$PATH
Setting Environment Variables on Windows
For how to modify system environment variables and modify the PATH
environment variable on Windows, please refer to online tutorials (Baidu or Google).
IDE Tool Configuration
This article uses the Goland
development tool as a base to introduce the configuration of common tools in this IDE.
Commonly used tools include:
go fmt
: A unified code formatting tool (mandatory).golangci-lint
: A static code quality inspection tool used for package quality analysis (recommended).goimports
: An automaticimport
dependency package tool (optional).golint
: A code convention inspection tool, which also detects the quality of single-file code and is used by the well-known Go quality assessment site Go Report (optional).
go fmt
, goimports
, golangci-lint
Since these three tools are built into Goland
, the configuration is relatively simple. Refer to the following pictorial operation example:
- In the
Goland
settings, selectTools
-File Watchers
, then choose to add
- Add these 3 tools in turn by clicking, and use the default configuration
- Subsequently, while writing code, these 3 tools will be automatically triggered for detection when you save the code files.
Installing and Configuring golint
Tool (Optional)
Installing golint
Since Goland
does not come with the golint
tool, you need to download and install it yourself first.
If you have configured goproxy, you can directly use go install golang.org/x/lint/golint@latest
to install, without needing the command below.
Use the following commands to install:
mkdir -p $GOPATH/src/golang.org/x/
cd $GOPATH/src/golang.org/x/
git clone https://github.com/golang/lint.git
git clone https://github.com/golang/tools.git
cd $GOPATH/src/golang.org/x/lint/golint
go install
After a successful installation, you will see the automatically generated golint
binary tool file in the $GOPATH/bin
directory.
Configuring golint
- Then in the
Tools
-File Watchers
configuration ofGoland
, by copying the configuration ofgo fmt
- Modify the three settings
Name
,Program
, andArguments
, whereArguments
needs to add the-set_exit_status
parameter, as shown in the figure:
- Save, and the
golint
tool will be automatically triggered for detection when saving the code during writing.
Configuring golangci-lint
(Optional)
- Then in the
Tools
-File Watchers
configuration ofGoland
, by copying thego fmt
configuration - Modify the
Name
,Program
, andArguments
settings, whereArguments
needs to add therun $FileDir$
parameter. Note: The options in\
Advanced Options`` can be deselected if the machine is relatively slow, as shown in the figure: - Save, and the
golangci-lint
tool will be automatically triggered for detection when saving the code during writing. - Manage the configuration of
golangci-lint
tools through thego Linter
plugin. Below is the installation and configuration ofgo Linter
.