在使用golang的go.mod時,正確使用遞歸替換是一個重要的問題。 php小編魚仔為您提供了一些解決方案。遞歸替換是指在go.mod檔案中將某個模組替換為本機目錄的方式,以便在開發過程中進行偵錯和修改。但是,如果替換的模組依賴其他模組,可能會導致一些問題。透過正確使用遞歸替換,您可以避免這些問題,並確保專案的依賴關係正常運作。在本文中,我們將討論如何在go.mod中正確使用遞歸替換,並提供一些實用的技巧和注意事項。
我想要一個專案結構,如下所示:
src/github.com/myname/myproject/ - tl: - constructor - someghing.go - go.mod: module github.com/myname/myproject/tl/constructor require github.com/myname/myproject v0.0.0 require github.com/myname/myproject/tl/types v0.0.0 require github.com/myname/myproject/tl/types/account v0.0.0 require github.com/myname/myproject/tl/types/auth v0.0.0 require github.com/myname/myproject/tl/types/bots v0.0.0 replace github.com/myname/myproject v0.0.0 => ./../.. replace github.com/myname/myproject/tl/types v0.0.0 => ./../types replace github.com/myname/myproject/tl/types/account v0.0.0 => ./../types/account replace github.com/myname/myproject/tl/types/auth v0.0.0 => ./../types/auth replace github.com/myname/myproject/tl/types/channels v0.0.0 => ./../types/channels - crcs - someghing.go - go.mod: module github.com/myname/myproject/tl/crcs - functions - account - someghing.go - go.mod: module github.com/myname/myproject/tl/functions/account require github.com/myname/myproject/tl/crcs v0.0.0 require github.com/myname/myproject v0.0.0 require github.com/myname/myproject/tl/types v0.0.0 replace github.com/myname/myproject/tl/crcs v0.0.0 => ./../../crcs replace github.com/myname/myproject v0.0.0 => ./../../.. replace github.com/myname/myproject/tl/types v0.0.0 => ./../../types - auth - someghing.go - go.mod: module github.com/myname/myproject/tl/functions/auth require github.com/myname/myproject/tl/crcs v0.0.0 require github.com/myname/myproject v0.0.0 require github.com/myname/myproject/tl/types v0.0.0 replace github.com/myname/myproject/tl/crcs v0.0.0 => ./../../crcs replace github.com/myname/myproject v0.0.0 => ./../../.. replace github.com/myname/myproject/tl/types v0.0.0 => ./../../types - bots - someghing.go - go.mod: module github.com/myname/myproject/tl/functions/bots require github.com/myname/myproject/tl/crcs v0.0.0 require github.com/myname/myproject v0.0.0 require github.com/myname/myproject/tl/types v0.0.0 replace github.com/myname/myproject/tl/crcs v0.0.0 => ./../../crcs replace github.com/myname/myproject v0.0.0 => ./../../.. replace github.com/myname/myproject/tl/types v0.0.0 => ./../../types - someghing.go - go.mod: module github.com/myname/myproject/tl/functions require github.com/myname/myproject/tl/crcs v0.0.0 require github.com/myname/myproject v0.0.0 require github.com/myname/myproject/tl/types v0.0.0 replace github.com/myname/myproject/tl/crcs v0.0.0 => ./../crcs replace github.com/myname/myproject v0.0.0 => ./../.. replace github.com/myname/myproject/tl/types v0.0.0 => ./../types - types - account - something.go - go.mod: module github.com/myname/myproject/tl/types/account require github.com/myname/myproject/tl/crcs v0.0.0 require github.com/myname/myproject v0.0.0 require github.com/myname/myproject/tl/types v0.0.0 replace github.com/myname/myproject/tl/crcs v0.0.0 => ./../../crcs replace github.com/myname/myproject v0.0.0 => ./../../.. replace github.com/myname/myproject/tl/types v0.0.0 => ./.. - auth - something.go - go.mod: module github.com/myname/myproject/tl/types/auth require github.com/myname/myproject/tl/crcs v0.0.0 require github.com/myname/myproject v0.0.0 require github.com/myname/myproject/tl/types v0.0.0 replace github.com/myname/myproject/tl/crcs v0.0.0 => ./../../crcs replace github.com/myname/myproject v0.0.0 => ./../../.. replace github.com/myname/myproject/tl/types v0.0.0 => ./.. - channels - something.go - go.mod: module github.com/myname/myproject/tl/types/channels require github.com/myname/myproject/tl/crcs v0.0.0 require github.com/myname/myproject v0.0.0 require github.com/myname/myproject/tl/types v0.0.0 replace github.com/myname/myproject/tl/crcs v0.0.0 => ./../../crcs replace github.com/myname/myproject v0.0.0 => ./../../.. replace github.com/myname/myproject/tl/types v0.0.0 => ./.. - someghing.go - go.mod: module github.com/myname/myproject/tl/types require github.com/myname/myproject/tl/crcs v0.0.0 require github.com/myname/myproject v0.0.0 replace github.com/myname/myproject/tl/crcs v0.0.0 => ./../crcs replace github.com/myname/myproject v0.0.0 => ./../.. - something.go - go.mod: module github.com/myname/myproject [some external dependencies] require github.com/myname/myproject/tl/types v0.0.0 replace github.com/myname/myproject/l/types v0.0.0 => ./tl/types
這不起作用。我使用 jetbrains 的 ide,它在所有(子)模組上執行 go list,並告訴我以下錯誤:
go: github.com/myname/myproject/tl/[email protected] requires github.com/myname/myproject/tl/[email protected]: reading github.com/myname/myproject/tl/crcs/go.mod at revision tl/crcs/v0.0.0: unknown revision tl/crcs/v0.0.0
我不明白為什麼會發生這種情況,看起來它嘗試從存儲庫中獲取模組,即使所有內容都在本地替換。
除了改變專案結構之外,我願意接受建議。我將來需要能夠使用 account.sometype
之類的東西,因此將所有內容放在一個文件中會很痛苦。
上下文:我正在為電報建立mtproto 庫,它具有tl 模式,其中每個「請求」/「類別」都在自己的命名空間中(例如「帳戶」、「通道」)(或在全域命名空間中,例如我是functions
和types
對應)(因為這些函數/類別有超過1900 個,每個函數/類別都有自己的建構子和參數,因此將所有內容都放在一個模組中將很難使用)
tl資料夾中的檔案是從tl模式自動產生的go程式碼。
感謝Dean指出您可以在一個模組中包含多個套件(如何?)。它解決了我的問題,同時仍然保持相同的結構。
以上是如何在 golang 的 go.mod 中正確使用遞歸替換?的詳細內容。更多資訊請關注PHP中文網其他相關文章!