Go related command instructions

go get: Download the source code of the third-party library and place it in the $GOPATH/src directory

  • Mainly used to download the source code of third-party libraries and place them in the $GOPATH/src directory.
  • If you use go get, it will download the code and store its source code in $GOPATH/src. Additionally, it checks and installs executables into $GOPATH/bin if third-party libraries contain executables.
  • In the latest version of Go, the function of go get has been integrated into other commands, and the introduction of Go Modules reduces the dependence on $GOPATH. Therefore, using go get can also install dependencies in the project, not necessarily outside the $GOPATH directory.
go get github.com/example/package

go install: Compile and install the Go program or library, and install the generated binary file into the $GOPATH/bin or ($GOPATH/pkg if it is a library) directory

  • Mainly used to compile and install Go programs or libraries, and install the generated binaries into the $GOPATH/bin (or $GOPATH/pkg if it is a library) directory.
  • If you use go install, it will compile and install the executable (or library) of the current directory or the specified package into the $GOPATH/bin (or $GOPATH/pkg) directory.
go install

go mod tidy: clean up unused dependencies and update module files

go mod tidy is a command in the Go language that is used to automatically clean and remove unused dependencies in module files. It is part of the Go module management tool and is used to maintain module dependencies, ensuring that only actually used dependencies are retained to reduce module size.

The main function

  1. Clean unused dependencies: This command analyzes your code to determine which dependencies are not actually referenced. It automatically removes these unused dependencies from the module file (go.mod).
  2. Update module files: Once cleanup is complete, go mod tidy updates module files to reflect the removed dependencies. This ensures that the module files stay in sync with your actual code.

benefit

  • Reduce binary size: By removing unused dependencies, you can reduce the size of the generated binary, thereby reducing the size of the executable.
  • Simplified dependency management: Keeping only the dependencies you actually use can make your dependency management clearer and streamlined.
  • Better performance: Smaller modules generally result in faster builds and fewer network downloads, which improves performance.

The command is similar to performing the following steps

  1. Check the project's go.mod file to determine the versions of the project's dependencies.
  2. For each dependency, check if it already exists, if not, use go get to download the dependency and add it to go.mod and go.sum.
  3. Remove dependencies that are no longer needed.

go clean -modcache: clear module cache

go clean -testcache: clear test cache

go test -v ./client: Test all test functions in the client directory in the current directory

Guess you like

Origin blog.csdn.net/trinityleo5/article/details/132625046