![](/img/trans.png)
[英]How to import gitsubmodule containing golang code with go.mod and go.sum file
[英]why I use golang module, and import a module that is not opted in module, but go.sum file has go.mod file hash?
我現在使用 golang 1.13 並使用 go 模塊。
但是,當我導入一個沒有在 go 模塊中選擇的包(例如,a)時,在 go.sum 文件中仍然有兩行。 Go 模塊告訴我們“每個已知的模塊版本都會在 go.sum 文件中生成兩行。第一行給出模塊版本文件樹的哈希值。第二行將“/go.mod”附加到版本並給出僅模塊版本(可能是合成的)go.mod 文件的哈希值。go.mod-only 哈希值允許下載和驗證模塊版本的 go.mod 文件,該文件是計算依賴圖所必需的,而無需下載所有模塊的源代碼代碼。”
( https://tip.golang.org/cmd/go/#hdr-Module_downloading_and_verification )。
但是這個包不是一個模塊,所以它沒有 go.mod 文件? 例如,如果我導入不是模塊的包調用“github.com/example/a”,在 go.sum 文件中,它仍然有這兩行:
github.com/example/a v0.0.0-20190627063042-31896c4e4162 h1:rSqi2vQEpS+GAFKrLvmxzWW3OGlLI4hANnEf/ib/ofo=
github.com/example/a v0.0.0-20190627063042-31896c4e4162/go.mod h1:tcpxll8wcruwpPpWBbjAsWc1JbLHld/v9F+3rgLIr4c=
我的問題是,第二行是如何生成的?
go.sum
包含特定模塊版本內容的預期加密校驗和。 每次使用依賴項時,如果缺少或需要匹配 go.sum 中的現有條目,則將其校驗和添加到 go.sum。
每個包/模塊都是依賴項,每個依賴項都意味着使用go.sum
校驗和進行維護,因此無論是包還是模塊,都將對其進行維護。
源代碼將相應地下載到$GOPATH/src
目錄中。
嘗試 -
導致在go.sum
文件中寫入每個依賴項 sum 哈希。 一個與您的go.mod
文件相關,另一個是從您導入的模塊中導入的。 嘗試運行go mod tidy
以減少導入的模塊,您的go.mod
文件將包含一些//indirect
import,這是您導入的模塊內部使用的。
或許Golang源碼可以解釋原因:
func (r *codeRepo) legacyGoMod(rev, dir string) []byte {
// We used to try to build a go.mod reflecting pre-existing
// package management metadata files, but the conversion
// was inherently imperfect (because those files don't have
// exactly the same semantics as go.mod) and, when done
// for dependencies in the middle of a build, impossible to
// correct. So we stopped.
// Return a fake go.mod that simply declares the module path.
return []byte(fmt.Sprintf("module %s\n", modfile.AutoQuote(r.modPath)))
}
來自這里的代碼: https : //github.com/golang/go/blob/acac535c3ca571beeb168c953d6d672f61387ef1/src/cmd/go/internal/modfetch/coderepo.go#L857
↓ VSCode 打開/usr/local/go/src
:
cmd/go/internal/modfetch/coderepo.go
,給 func legacyGoMod
添加斷點cmd/go/internal/modfetch/coderepo_test.go
,按 F5
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.