具有多个模块的 mono-repos 中是否有 Go 模块名称的约定?
Are there conventions for Go module names in mono-repos with multiple modules?
在多模块存储库中,模块名称(通过 go.mod module
指令设置)是否应遵循包命名约定?
例如module github.com/org-name/repo-name/path/to/module-dir
我了解到,无论模块的名称是什么,模块中的包都使用模块名称作为前缀来相互引用。但是,从模块外部来看,如果将模块名称设置为 <host><path-within-repo>
模式以外的名称,似乎会出现问题。 get
-ing 包含在模块中的包然后给出关于 unrecognized import path
的消息。
是否有任何原因使模块命名不同于 <host><path-within-repo>
?
对引用 mod 规则没有任何硬性要求,尽管使用 domain/repo 模式总是好的做法。所以,如果你想在本地引用其他 mod 不在 GOPATH 中的规则,你可以使用 replace
指令。
https://github.com/golang/go/wiki/Modules#when-should-i-use-the-replace-directive
replace also can be used to inform the go tooling of the relative or absolute on->disk location of modules in a multi-module project, such as:
replace example.com/project/foo => ../foo
假设我们有以下结构:
├── .gitignore
├── pkg1
│ ├── go.mod
│ └── main.go
└── pkg2
├── go.mod
└── utils.go
pkg1/main.go
package main
import (
"fmt"
"local/pkg2"
)
func main() {
fmt.Println(pkg2.Add(1, 2))
}
pkg1/go.mod
module local/pkg1
go 1.12
require local/pkg2 v0.0.0
replace local/pkg2 => ../pkg2
pkg2/utils.go
package pkg2
func Add(a, b int) int {
return a + b
}
pkg2/go.mod
module local/pkg2
go 1.12
运行:
cd pkg1
go run main.go
你得到:
3
如果您希望能够 go get
一个模块,它应该遵循 <host>/repo/path/within/repo
模式。
但是,我建议退后一步,询问您是否真的想要一个多模块存储库。它增加了相当大的复杂性,很难做到正确,并且通常意味着在持续的基础上需要做更多的工作。
Russ Cox 在 #26664 中评论:
For all but power users, you probably want to adopt the usual convention that one repo = one module. It's important for long-term evolution of code storage options that a repo can contain multiple modules, but it's almost certainly not something you want to do by default.
有关详细信息,请参阅 。
在多模块存储库中,模块名称(通过 go.mod module
指令设置)是否应遵循包命名约定?
例如module github.com/org-name/repo-name/path/to/module-dir
我了解到,无论模块的名称是什么,模块中的包都使用模块名称作为前缀来相互引用。但是,从模块外部来看,如果将模块名称设置为 <host><path-within-repo>
模式以外的名称,似乎会出现问题。 get
-ing 包含在模块中的包然后给出关于 unrecognized import path
的消息。
是否有任何原因使模块命名不同于 <host><path-within-repo>
?
对引用 mod 规则没有任何硬性要求,尽管使用 domain/repo 模式总是好的做法。所以,如果你想在本地引用其他 mod 不在 GOPATH 中的规则,你可以使用 replace
指令。
https://github.com/golang/go/wiki/Modules#when-should-i-use-the-replace-directive
replace also can be used to inform the go tooling of the relative or absolute on->disk location of modules in a multi-module project, such as:
replace example.com/project/foo => ../foo
假设我们有以下结构:
├── .gitignore
├── pkg1
│ ├── go.mod
│ └── main.go
└── pkg2
├── go.mod
└── utils.go
pkg1/main.go
package main
import (
"fmt"
"local/pkg2"
)
func main() {
fmt.Println(pkg2.Add(1, 2))
}
pkg1/go.mod
module local/pkg1
go 1.12
require local/pkg2 v0.0.0
replace local/pkg2 => ../pkg2
pkg2/utils.go
package pkg2
func Add(a, b int) int {
return a + b
}
pkg2/go.mod
module local/pkg2
go 1.12
运行:
cd pkg1
go run main.go
你得到:
3
如果您希望能够 go get
一个模块,它应该遵循 <host>/repo/path/within/repo
模式。
但是,我建议退后一步,询问您是否真的想要一个多模块存储库。它增加了相当大的复杂性,很难做到正确,并且通常意味着在持续的基础上需要做更多的工作。
Russ Cox 在 #26664 中评论:
For all but power users, you probably want to adopt the usual convention that one repo = one module. It's important for long-term evolution of code storage options that a repo can contain multiple modules, but it's almost certainly not something you want to do by default.
有关详细信息,请参阅