1. Basic commands of GO

go mod command 

go mod download    下载依赖的module到本地cache(默认为$GOPATH/pkg/mod目录)
go mod edit        编辑go.mod文件
go mod graph       打印模块依赖图
go mod init        初始化当前文件夹, 创建go.mod文件
go mod tidy        增加缺少的module,删除无用的module
go mod vendor      将依赖复制到vendor下
go mod verify      校验依赖
go mod why         解释为什么需要依赖

1.go mod init project_name

go mod initCommand is used to initialize a new Go module. It will create a new file in the current directory go.mod, which contains the name, version and other information of the module, as well as information about other modules on which the module depends. Files are required when using go modManage Dependencies .go.mod

2. go mod tidy

go mod tidycommand to tidy up and clean up a project's go.modfiles and dependencies. Its main purpose is to remove unused dependencies, update the versions of dependencies, and keep the go.modlist of dependencies in the file consistent with the list of dependencies actually used.

When we develop Go projects, we usually use third-party packages or libraries. These dependencies are documented in the project's go.modfile. However, sometimes we may add some dependencies and end up not using them. These unused dependencies will increase the project size and build time, so they need to be removed. In addition, when there are new versions of the dependencies we use, we also need to update the versions of the dependencies for better performance and security. 

go mod tidyThe command automatically analyzes the code and dependencies of the project, finds unused dependencies and removes them, and updates the versions of dependencies. It also checks go.modwhether the dependency list in the file is consistent with the actual dependency list used, and if not, automatically updates the go.moddependency list in the file. This ensures that the project's dependency list is always up to date and accurate.

3. go build project_name

go build allows us to create a binary program, and we can use this package in a new environment even without GO. But this binary program is also different under different operating systems. as follows:

If yours Windowsis using cmdthen specify the environment variable as follows.

SET CGO_ENABLED=0  // 禁用CGO
SET GOOS=linux  // 目标平台是linux
SET GOARCH=amd64  // 目标处理器架构是amd64

If you Windowsare using PowerShella terminal, the syntax for setting environment variables is

$ENV:CGO_ENABLED=0
$ENV:GOOS="linux"
$ENV:GOARCH="amd64"

WindowsAfter executing the above command in your terminal, execute the following command to get an executable file that can run on the Linux platform.

go build

Compiling Mac executables for Windows

Compile 64-bit executable programs for Mac platform under Windows:

Execute in cmd terminal:

SET CGO_ENABLED=0
SET GOOS=darwin
SET GOARCH=amd64
go build

Execute in PowerShell terminal:

$ENV:CGO_ENABLED=0
$ENV:GOOS="darwin"
$ENV:GOARCH="amd64"
go build

Compile Linux executable files for Mac

Compile on a Mac computer to obtain a 64-bit executable program on the Linux platform:

CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build

Compile Windows executables for Mac

Compile on a Mac computer to obtain a 64-bit executable program on the Windows platform:

CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build

Compiling Mac executables on Linux

Compile the Mac platform 64-bit executable program under the Linux platform:

CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build

Linux compiles Windows executable files

Compile Windows platform 64-bit executable program under Linux platform:

CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build

 4. go get

Executing the command in the project go getcan download the dependency package, and you can also specify the downloaded version.

  1. Running go get -uwill upgrade to the latest minor version or revision (xyz, where z is the revision number and y is the minor version number)
  2. Running go get -u=patchwill upgrade to the latest revision
  3. Running go get package@versionwill upgrade to the specified version number version

If you download all dependencies you can use go mod downloadthe command.

5. go mod edit

format

Because we can manually modify the go.mod file, sometimes we need to format the file. Go provides the following commands:

go mod edit -fmt

Add dependencies

go mod edit -require=golang.org/x/text

Remove dependencies

If you just want to modify go.modthe content in the file, you can run it go mod edit -droprequire=package path. For example, to go.modremove golang.org/x/textthe package in , you can use the following command:

go mod edit -droprequire=golang.org/x/text

More usage information go mod editcan be go help mod editfound through .

 Most of the article is reproduced:  Dependency Management of Go Language | Li Wenzhou's Blog

Guess you like

Origin blog.csdn.net/qq_48480384/article/details/129915661