Go commands

Time:2024-1-16

preamble

Continuing on from the previous post, we’ll cover common commands in the Go language.

Common commands

Here are some common Go commands that can help you with tasks such as compiling, testing, running, and managing dependencies in Go development.
order descriptive
go build Compile Go packages and their dependencies.
go test Run the package test.
go run Compile and run the Go program.
go clean Delete the object files and cache files generated by the compilation.
go bug Launch a tool for reporting bugs.
go doc Displays documentation about the package or symbol.
go env Prints information about the Go environment.
go fix Update the package to use the new API.
go fmt Reformat the source code of Go packages using gofmt.
go generate Generate Go files by processing the source code.
go get Add dependencies to the current module and install them.
go install Compile and install the package and its dependencies.
go list Lists information about the package or module.
go mod Used for module maintenance , including initialization of the module , add and update dependencies .
go work Used for workspace maintenance, such as viewing, cleaning or printing workspace information.
go tool Run the specified Go tool.
go version Prints Go’s version information.
go vet Check the Go source code and report suspicious errors.

go bug
The go bug command is used to report Go language bugs or provide feedback. It will automatically collect some information about your Go environment and system and send that information to the official bug tracking system for the Go language. The general format for using the go bug command is as follows:
go bug [package]
where [package] is an optional parameter that specifies the name of the package in which the problem occurs. If this parameter is not specified, the go bug command will default to the package name of the current directory.
typical example
1. Report a bug
go bug
This command collects information about your Go environment and system, and opens a browser page for you to fill in the details of the bug report. 2. Report a bug in a specific package
go bug github.com/example/package
This command collects information about your Go environment and system, and opens a browser page that lets you fill in details about bug reports for the github.com/example/package package. Note that the go bug command automatically collects some information about your Go environment and system, including Go version, operating system, CPU architecture, and so on. When filling out the bug report, you need to provide as much detailed information as possible, including reproduction steps, expected results, actual results, etc., to help developers better understand and solve the problem.
Parameter description
parameters clarification
[package] An optional parameter that specifies the name of the package in which the problem occurred. If this parameter is not specified, the go bug command will default to the package name of the current directory.
-h Displays help information.
-v Displays detailed debugging information.
-e Edit the bug report template before reporting the bug.
-f Edit the bug report template before reporting the bug and save the template to the specified file.
-json Outputs the collected information in JSON format.
-m Edit the bug report template before reporting the bug and save the template to the clipboard.
-n Only information is collected, no browser page is opened.
-o Specifies the path to the output file.
-p Specifies the proxy to be used.
-r Specify the URL of the bug tracking system to use.
Note that the parameters of the go bug command can be used in combination to meet different needs. For example, you can use the go bug -e command to edit a bug report template and automatically open a browser page for bug report submission after editing.
go doc
The go doc command is a tool command in the Go language for viewing the documentation of Go program entities (such as packages, functions, types, etc.). It displays the entity’s comments, signatures, methods, constants, variables, and other information to help developers better understand and use the Go language’s standard and third-party libraries. The general format for using the go doc command is as follows:
go doc [package] [entity]
where [package] is an optional parameter that specifies the package name of the document to be viewed. If this parameter is not specified, the go doc command defaults to the package name of the current directory. [entity] is an optional parameter that specifies the name of the entity to view the documentation for. If you do not specify this parameter, the go doc command displays the documentation for the entire package.
typical example
1. View the documentation for the entire package
go doc fmt
This command displays the documentation for the fmt package, including package comments, exported functions, exported types, exported constants, and other information. 2. View entity-specific documents
go doc fmt.Println
This command displays the documentation for the Println function in the fmt package, including information about the function’s comments, arguments, return values, and so on. Note that the go doc command can be used to view documentation for both standard and third-party libraries. For standard libraries, you can directly use the package name as a parameter; for third-party libraries, you need to use the go get command to install the library, and then use the go doc command to view the documentation.
Parameter description
parameters clarification
[package] Optional parameter that specifies the package name of the document to be viewed. If this parameter is not specified, the go doc command defaults to the package name of the current directory.
[entity] Optional parameter that specifies the name of the entity to view the document. If this parameter is not specified, the go doc command displays the documentation for the entire package.
-all Displays documentation for all entities, including those that are not exported.
-c Displays the full signature of the entity, including the types of parameters and return values.
-cmd Displays command documentation, including command usage and parameter descriptions.
-u Displays the URL of the entity, which can be opened in a browser.
-src Displays the source code of the entity.
-v Displays detailed documentation information, including comments, methods, constants, variables, and more.
-short Run short tests. Use this parameter to run tests for a short period of time, e.g. to skip some time-consuming test cases.
Note that the arguments to the go doc command can be used in combination to meet different needs. For example, you can use the go doc -c command to display the full signature of an entity, or the go doc -v command to display detailed documentation information.
go env
The go env command is used to display information about Go environment variables. It displays information about the Go language compiler and runtime configurations, including GOROOT, GOPATH, GOOS, GOARCH, and so on. The general format for using the go env command is as follows:
go env [variable]
where [variable] is an optional parameter that specifies the environment variables to view. If you do not specify this parameter, the go env command displays all environment variables.
typical example
1. View all environment variables:
go env
This command displays all Go environment variables, including GOROOT, GOPATH, GOOS, GOARCH, and so on. 2. View specific environment variables:
go env GOROOT
This command displays the value of the GOROOT environment variable, which is the path to the Go language installation.
go fix
The go fix command is used to automatically fix compatibility issues in older versions of Go code. It can automatically update the syntax and API calls in the code according to the changes in the Go language version to make it compatible with the new version of the Go language. The general format for using the go fix command is as follows:
go fix [packages]
where [packages] is an optional parameter that specifies the packages to be fixed. If this parameter is not specified, the go fix command fixes all packages in the current directory by default.
typical example
1. Fix all packages in the current directory:
go fix
This command automatically fixes all packages in the current directory, updating them to code compatible with the current version of the Go language. 2. Repair the specified package:
go fix package1 package2
This command automatically fixes the specified package, updating it to code compatible with the current version of the Go language. It is important to note that the go fix command can only fix compatibility issues in older versions of Go code and cannot solve all code problems. Before using this command, it is recommended to back up your code in case something unexpected happens during the fixing process.
go fmt
The go fmt command is a tool command in the Go language for formatting Go code. It can automatically adjust the code indentation, spaces, line breaks and other formats, so that the code has a unified style and improve the readability of the code. The general format for using the go fmt command is as follows:
go fmt [packages]
where [packages] is an optional parameter that specifies the packages to be formatted. If this parameter is not specified, the go fmt command formats all packages in the current directory by default.
typical example
1. Format all packages in the current directory:
go fmt
This command automatically formats the code in all packages in the current directory to conform to the Go language code style. 2. Format the specified package:
go fmt package1 package2
This command automatically formats the code in the specified package to conform to the Go language code style. Note that the go fmt command directly modifies the source code file, so it is recommended to back up the code before using this command in case something unexpected happens during the formatting process.
go generate
The go generate command is a tool command in the Go language for automated code generation. It triggers the code generation process by adding specific comments to Go source files, which can be used to generate repetitive code and reduce the amount of manual writing. The general format for using the go generate command is as follows:
go generate [packages]
where [packages] is an optional parameter that specifies the packages for which code generation is to be performed. If this parameter is not specified, the go generate command executes code generation from all packages in the current directory by default.
typical example
1. Add comments to the generated code in the Go source file:
//go:generate command arguments
Add a comment like this to the source file, where command is the command to be executed and arguments are the arguments to the command. 2. Execute code generation:
go generate
This command automatically performs code generation from all packages in the current directory, based on the commands and arguments in the comments. Note that the go generate command performs code generation based on the commands and arguments in the comments, so you need to make sure that you are using the correct commands and arguments before using this command.

summarize

This article introduces the go commands go bug, go doc, go env, go fix, go fmt, and go generate, with examples and parameter descriptions, and the next chapter describes the remaining commands.

put at the end

Thank you for your support and encouragement! If you are interested in related articles, you can pay attention to the public number “Architecture Hall”, will continue to update the AIGC, java basic interview questions, netty, spring boot, spring cloud series of articles, a series of dry goods at any time delivery!

Recommended Today

Resolved the Java. SQL. SQLNonTransientConnectionException: Could not create connection to the database server abnormal correctly solved

Resolved Java. SQL. SQLNonTransientConnectionException: Could not create connection to the database server abnormal correct solution, kiss measuring effective!!!!!! Article Catalog report an error problemSolutionscureexchanges report an error problem java.sql.SQLNonTransientConnectionException:Could not create connection to database server Solutions The error “java.sql.SQLNonTransientConnectionException:Could not create connection to database server” is usually caused by an inability to connect to the […]