日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

go?install和go?get的區別實例詳解_Golang

作者:catmes ? 更新時間: 2023-03-22 編程語言

go get 和 go install 的區別

先看結論:

  • go get: 對?go mod?項目,添加,更新,刪除?go.mod?文件的依賴項(僅源碼)。不執行編譯。側重應用依賴項管理。
  • go install: 在操作系統中安裝?Go 生態的第三方命令行應用。不更改項目?go.mod?文件。側重可執行文件的編譯和安裝。

之前網上亂傳的 go get 命令要被棄用是錯的。正確說法是,go 1.17后,go get 命令的使用方式發生了改變.

具體什么改變呢?請看官方說明:

Starting in Go 1.17, installing executables with go get is deprecated. go install may be used instead.
In Go 1.18, go get will no longer build packages; it will only be used to add, update, or remove dependencies in go.mod.
Specifically, go get will always act as if the -d flag were enabled.

大概表達3個意思:

  • 自?Go 1.17?起, 棄用?go get?命令安裝可執行文件,使用?go install?命令替代.
  • Go 1.18起,go get?命令不再有編譯包的功能。將只有添加,更新,移除?go.mod?文件中的依賴項的功能。
  • go get?命令將默認啟用?-d?選項。

go get命變更

  • Go 1.17 之前:go get 通過遠程拉取或更新代碼包及其依賴包,并自動完成編譯和安裝。實際分成兩步操作:1. 下載源碼包,2. 執行 go install。
  • Go 1.17 之后: 棄用go get命令的編譯和安裝功能

go get命令變更的原因

由于 go 1.11 之后 go mod modules特性的引入,使得go get 命令,既可以安裝第三方命令,又可以從 go.mod 文件自動更新項目依賴。但多數情況下,開發者只想做二者之一。

go 1.16 起,go install 命令,可以忽略當前目錄的 go.mod文件(如果存在),直接安裝指定版本的命令行應用。

go get 命令的編譯和安裝功能,因為和 go install 命令的功能重復,故被棄用。由于棄用了編譯和安裝功能,go get 命令將獲得更高的執行效率, 也不會在更新包的時候,再出現編譯失敗的報錯。

Since modules were introduced, the go get command has been used both to update dependencies in go.mod and to install commands.
This combination is frequently confusing and inconvenient: in most cases, developers want to update a dependency or install a command but not both at the same time.

Since Go 1.16, go install can install a command at a version specified on the command line while ignoring the go.mod file in the current directory (if one exists).
go install should now be used to install commands in most cases.

go get’s ability to build and install commands is now deprecated, since that functionality is redundant with go install.
Removing this functionality will make go get faster, since it won’t compile or link packages by default.
go get also won’t report an error when updating a package that can’t be built for the current platform.

go get 由于具備更改 go.mod 文件的能力,因此我們 必須要避免執行 go get 命令時,讓它接觸到我們的 go.mod 文件 ,否則它會將我們安裝的工具作為一個依賴。

所以,如果不是為了更新項目依賴,而是安裝可執行命令,請使用 go install

GOMODULE常用命令

go mod init  # 初始化go.mod
go mod tidy  # 直接從源代碼中獲取依賴關系,更新依賴文件。可刪掉go.mod中無用的依賴。
go mod download  # 下載依賴文件
go mod vendor  # 將依賴轉移至本地的vendor文件
go mod edit  # 手動修改依賴文件
go mod graph  # 打印依賴圖
go mod verify  # 校驗依賴

在項目源碼中使用 import 語句,導入新的依賴模塊前,可用 go get 命令,先下載新模塊。

go instsll 應該在module外部使用 https://github.com/golang/go/issues/40276
棄用go get命令安裝可執行文件 https://go.dev/doc/go-get-install-deprecation
Go 1.16 中關于 go get 和 go install https://cloud.tencent.com/developer/article/1766820

總結?

原文鏈接:https://blog.csdn.net/WHQ78164/article/details/128081577

欄目分類
最近更新