我的檔案夾結構看起來像這樣......(比如我的 git repo 名稱是 demorepo)
demorepo
|--directory1
|-- (no go.mod at this level)
|--module1
|--package1 --------->--------------->--------------------->----|
|--go.mod (github.com/demorepo/directory1/module1) |
|--go.sum |
|--module2 |
|--module3 |
|--directory2 |
|-- (no go.mod at this level) |
|--newmodule ------<------------<------------------<----------------|
現在,我想在我的“newmodule”中使用“package1”中定義的函式
當我在“new_module”點擊 go get <repo_address>/directort1/module1/package1 時,它說......
github.com/<repo>@upgrade found (v0.0.0-20211215055943-92e412ad4a12), but does not contain package github.com/<repo>/directory1/module1/package1
uj5u.com熱心網友回復:
有一個關于Go 1.18的 Go Workspace File的提議,它應該可以簡化這個任務。
同時,您可以使用replace指令在go.mod檔案中提及到位于本地檔案系統的模塊。
demorepo/directory1/module1/go.mod:
module github.com/<repo>/directory1/module1
demorepo/directory2/newmodule/go.mod:
module github.com/<repo>/directory2/newmodule
replace github.com/<repo>/directory1/module1 => ../../directory1/module1
現在你可以正常import github.com/<repo>/directory1/module1/package1進入newmodule,它會參考本地module1.
您可能不需要檔案本身中的replace指令go.mod,而是制作它的副本,例如go.mod.local,在構建專案時使用它:(go build -modfile go.mod.local .也可以添加*.local到.gitignore)。
uj5u.com熱心網友回復:
方法 :
如果你想在module1里面使用,newModule那么你應該做一個新的repo,module1把你的邏輯放在那里,把它推進去github。請確保你應該使用適合version圖書館。
import它作為一個library,它會作業。
另請參閱模塊依賴項的官方檔案并檢查根級別依賴項。
官方檔案:https : //go.dev/blog/using-go-modules
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/388912.html
