php小編百草為你介紹如何使用go工作區在本機載入相依性。在開發Go語言專案時,我們經常使用到各種第三方函式庫和相依性。為了方便管理和載入這些依賴項,Go語言提供了一個強大的工作區(workspace)機制。透過設定工作區路徑和使用go mod指令,我們可以快速、方便地載入和管理專案所需的依賴項,提高開發效率和程式碼品質。下面我們就來詳細了解如何使用go工作區在本機載入相依性。
問題內容
我有一個 go 工作區項目,其中包含一些 go 模組。一個模組依賴於同一工作區中的另一個模組。我希望在本地解決依賴關係,而無需從互聯網下載。
我有兩個模組,hello
和 example
。 hello
依賴 example
。其結構為:
example - go.mod - hello - go.mod hello - go.mod go.work
go.work
是:
go 1.19 use ( ./example/hello ./hello )
模組 hello
需要使用從 example -> hello
模組宣告的函數。 example/hello
下的go.mod
為:
module github.com/example/hello go 1.19
當我在 hello
目錄下運行 go get github.com/example/hello
時,出現錯誤:
go: module github.com/example/hello: git ls-remote -q origin in /Users/joey/go/pkg/mod/cache/vcs/85c69767672480b072ae4eaec76fbf39ef710d025b14e923dd131ac218c9034c: exit status 128: fatal: could not read Username for 'https://github.com': terminal prompts disabled Confirm the import path was entered correctly. If this is a private repository, see https://golang.org/doc/faq#git_https for additional information.
看來go
仍然想從github.com
下載依賴項而不是查看本地worksapce。有沒有辦法強制 go
先查看本地目錄?
完整程式碼為:https://github.com/zhaoyi0113/go-workspace
#解決方法
go.mod 檔案中的模組名稱與專案結構相關。否則,隨機命名可能會導致與其他庫發生衝突,從而導致在建置依賴項時不清楚要拉取哪個庫。
換句話說,目錄結構必須匹配才能從 GitHub 取得程式碼。因此,將“example”目錄下的go.mod檔案中的模組名稱更改為:
module github.com/zhaoyi0113/go-workspace/example
對於「hello」目錄:
module github.com/zhaoyi0113/go-workspace/hello
如果「hello」目錄需要依賴「example」模組,則導入如下:
require ( github.com/zhaoyi0113/go-workspace/example xxxx )
這裡,如果您建立了版本,「xxxx」可以是版本號。如果沒有,您可以使用推送到 GitHub 的程式碼中的 Git 提交雜湊。您可以在 GitHub 專案頁面上找到「example」的 Git 雜湊。在「hello」目錄下,執行以下指令:
go get github.com/zhaoyi0113/go-workspace/example@{git version prefix, which can be found on the GitHub project page}
但是,這種方法只允許您取得已推送到 GitHub 的程式碼。如果你想在本地快速調試開發,可以在go.mod檔中加入「replace」指令:
replace github.com/zhaoyi0113/go-workspace/example => {local directory of your example}
更新
建議使用套件方法組織專案結構,其中模組應該更加獨立,並且通常位於不同的儲存庫中。
. ├── example │ ├── reverse │ └── utils.go ├── hello │ └── hello.go ├── go.mod ├── go.sum └── main.go
以上是如何使用go工作區在本機載入相依性?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Toensureinitfunctionsareeffectiveandmaintainable:1)Minimizesideeffectsbyreturningvaluesinsteadofmodifyingglobalstate,2)Ensureidempotencytohandlemultiplecallssafely,and3)Breakdowncomplexinitializationintosmaller,focusedfunctionstoenhancemodularityandm

goisidealforbeginnersandsubableforforcloudnetworkservicesduetoitssimplicity,效率和concurrencyFeatures.1)installgromtheofficialwebsitealwebsiteandverifywith'.2)

開發者應遵循以下最佳實踐:1.謹慎管理goroutines以防止資源洩漏;2.使用通道進行同步,但避免過度使用;3.在並發程序中顯式處理錯誤;4.了解GOMAXPROCS以優化性能。這些實踐對於高效和穩健的軟件開發至關重要,因為它們確保了資源的有效管理、同步的正確實現、錯誤的適當處理以及性能的優化,從而提升軟件的效率和可維護性。

Goexcelsinproductionduetoitsperformanceandsimplicity,butrequirescarefulmanagementofscalability,errorhandling,andresources.1)DockerusesGoforefficientcontainermanagementthroughgoroutines.2)UberscalesmicroserviceswithGo,facingchallengesinservicemanageme

我們需要自定義錯誤類型,因為標準錯誤接口提供的信息有限,自定義類型能添加更多上下文和結構化信息。 1)自定義錯誤類型能包含錯誤代碼、位置、上下文數據等,2)提高調試效率和用戶體驗,3)但需注意其複雜性和維護成本。

goisidealforbuildingscalablesystemsduetoitssimplicity,效率和建築物內currencysupport.1)go'scleansyntaxandaxandaxandaxandMinimalisticDesignenhanceProductivityAndRedCoductivityAndRedCuceErr.2)ItSgoroutinesAndInesAndInesAndInesAndineSandChannelsEnablenableNablenableNableNablenableFifficConcurrentscorncurrentprogragrammentworking torkermenticmminging

Initfunctionsingorunautomationbeforemain()andareusefulforsettingupenvorments和InitializingVariables.usethemforsimpletasks,避免使用輔助效果,andbecautiouswithTestingTestingTestingAndLoggingTomaintAnainCodeCodeCodeClarityAndTestesto。

goinitializespackagesintheordertheordertheyimported,thenexecutesInitFunctionswithinApcageIntheirdeFinityOrder,andfilenamesdetermineTheOrderAcractacractacrosmultiplefiles.thisprocessCanbeCanbeinepessCanbeInfleccessByendercrededBydeccredByDependenciesbetenciesbetencemendencenciesbetnependendpackages,whermayleLeadtocomplexinitialitialializizesizization


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

Dreamweaver Mac版
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具