In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the knowledge of "how to understand the usage of the official tool chain of Go". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Go official tool chain
In order to run tool commands in the Go official tool chain from any directory (via the go command), the bin subdirectory path under the Go official tool chain installation directory must be configured in the PATH environment variable. When you install the Go official tool chain using the installer, it is likely that the installer has already configured this automatically. In the windows environment, you need to add the bin subdirectory under the installation directory to the advanced system environment variable to save it.
Recent versions of the Go official toolchain support a feature called Go module (Go modules) to manage project dependencies. This feature was introduced tentatively in version 1.11 and is supported by default in version 1.16.
The first environment variable
We should know about an environment variable: GOPATH. The default value for this environment variable is the directory path named go folder under the current user's HOME directory. The GOPATH environment variable can be configured with multiple paths manually. Later, when the GOPATH folder is mentioned, it represents the folder corresponding to the first path in the GOPATH environment variable.
The Go subfolder in the GOPATH folder is used to cache the version of the Go module (a PKG module is a collection of several Go library packages) that is dependent on the local project.
The GOBIN environment variable is used to specify where the Go application binary executable 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 Go official tool chain tool requires that all Go source code files end with the .go suffix. Here, let's assume that the simplest Go program is placed in a file in hello.go. Here is the simplest Go program.
Package mainimport "fmt" func main () {fmt.Println ("hello")}
Open a terminal (console) and go to the directory where the above source files are located, and then run
$go run hello.go
If there are syntax errors in the code, these errors will be output to the terminal.
If a program has several Go source code files in its main package, we can run the program using the following command.
$go run.
Note:
The go run subcommand is not recommended for use in formal large projects. The go runsubcommand is just a convenient way to run simple Go programs. For formal projects, it is best to use the go build or go install subcommands to build an executable file to run the Go program.
A go.mod file is required in the root directory of the Go project that supports the Go module feature. This file can be generated using the go mod in subcommand (see below).
More go subcommands
The three go subcommands mentioned above (go run, go build, and go install) will only output code syntax errors. They do not output possible code logic errors (that is, warnings). The go Vet subcommand can be used to check for possible code logic errors (that is, warnings).
We can use the go FMT subcommand to format Go code in the same code style.
We can use the go test subcommand to run the unit and benchmark cases.
We can use the go docsubcommand to view the documentation of the Go code library package in the terminal.
It is highly recommended that your Go project support the Go module feature to simplify dependency management. For a project that supports Go module features:
The go mod init example.com/myproject command can be used to generate a go.mod file in the current directory. The current directory will be treated as the root of a module named example.com/myproject (that is, the current project). This go.mod file will be used to record the dependent module and version information required by the current project. We can edit this file manually or use the go subcommand to modify this file.
The go mod tidy command is used to add unrecorded dependencies to the go.mod file or to remove dependencies that are no longer used from the go.mod file by scanning all the code in the current project.
The go get command uses pull to add, upgrade, demote, or delete individual dependencies. This command is not as common as the go mod tidy command.
Starting with version 1.16 of the official Go toolchain, we can run go install example.com/program@latest to install the latest version of a third-party Go program (to the GOBIIN directory). Prior to Go's official toolchain version 1.16, the corresponding command was go get-u example.com/program (now obsolete and no longer recommended).
We can run go help aSubCommand to see the help information for a subcommand aSubCommand.
Running the go command with no arguments will list all supported go subcommands.
This is the end of "how to understand the usage of the Go official tool chain". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.