Go official tool chain
In order to run the tool commands in the Go official toolchain from any directory (through the go command), the bin subdirectory path in the Go official toolchain installation directory must be configured in the PATH environment variable. When installing the official Go toolchain using the installer, the installer will most likely have automatically configured this. In the Windows environment, you need to add the bin subdirectory under the installation directory to the advanced system environment variables to save and take effect.
Recent versions of the Go official toolchain have supported a feature called Go modules to manage project dependencies. This feature was introduced experimentally in version 1.11 and was supported by default in version 1.16.
The first environment variable
We should understand an environment variable: GOPATH. The default value of this environment variable is the directory path corresponding to the go folder under the current user's HOME directory. GOPATH environment variables can be manually configured with multiple paths. Later, when the GOPATH folder is mentioned, it represents the folder corresponding to the first path in the GOPATH environment variable.
- The pkg subfolder in the GOPATH folder is used to cache the version of the Go module (a Go module is a collection of several Go library packages) that is dependent on by the local project.
- The GOBIN environment variable is used to specify where the Go application binary executable file generated by the go install subcommand should be stored. Its default value is the directory path corresponding to the bin subdirectory in the GOPATH folder.
- The GOBIN path needs to be configured in the PATH environment variable to run these Go applications from any directory.
Run a program
The official Go toolchain tool requires that all Go source code files must end with a .go suffix. Here, let's assume that a simplest Go program is placedin the file. Here is the easiest Go program.
package main import "fmt" func main() { ("hello") }
Open a terminal (console) and go to the directory where the above source file is located, and then run$ go run
If there are syntax errors in the code, these errors will be output in the terminal.
If a program has several Go source code files in its main package, we can run this program using the following command.$ go run .
Notice:
- The go run subcommand is not recommended for official large projects. The go run subcommand is just a convenient way to run a simple Go program. For formal projects, it is best to use the go build or go install subcommand to build an executable program file to run Go programs.
- A file is required in the root directory of a Go project that supports Go module features. This file can be generated using the go mod init subcommand (see below).
More go subcommands
The three go subcommands mentioned above (go run
、go build
andgo install
) will only output code syntax errors. They do not output possible code logic errors (i.e. warnings).go vet
Subcommands can be used to check for possible code logic errors (i.e. warnings).
We can usego fmt
Subcommands to format Go code in the same code style.
We can usego test
Subcommands to run unit and benchmark cases.
We can usego doc
Subcommands view the documentation of the Go codebase package in the terminal.
It is highly recommended to enable your Go project to support Go module features to simplify dependency management. For a project that supports Go module features:
- The go mod init /myproject command can be used to generate a file in the current directory. The current directory will be treated as the root directory of a module named /myproject (i.e. the current project). This file will be used to record the dependency module and version information required by the current project. We can edit this file manually or use the go subcommand to modify it.
- The go mod tidy command is used to add unrecorded dependencies to or delete dependencies that are no longer used by scanning all code in the current project.
- The go get command uses pull to add, upgrade, downgrade, or delete a single dependency. This command is not as commonly used as the go mod tidy command.
- Starting from the official Go toolchain version 1.16, we can run go install/program@latestTo install the latest version of a third-party Go program (to the GOBIIN directory). Before Go official toolchain version 1.16, the corresponding command was go get -u /program (now it has been abandoned and is no longer recommended to be used).
We can rungo help aSubCommand
Let's view the help information of aSubCommand subcommand.
Run without parametersgo
The command will list all supported go subcommands.
This is the end of this article about the introduction to the usage of Go official toolchain. For more related Go toolchain content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!