英文:
How to import and build with a group of local modules in Go
问题
我有以下文件:
$GOPATH/src/example.com
├── a
│ ├── a.go
│ └── go.mod
├── b
│ ├── b.go
│ └── go.mod
└── c
├── go.mod
└── main.go
文件内容如下:
a/a.go
:
package a
func NewA() int {
return 1
}
a/go.mod
:
module example.com/a
go 1.17
b/b.go
:
package b
import A "example.com/a"
func NewB() int {
return A.NewA()
}
b/go.mod
:
module example.com/b
go 1.17
replace example.com/a => ../a
require example.com/a v0.0.0-00010101000000-000000000000 // indirect
c/main.go
:
package main
import "fmt"
import B "example.com/b"
func main() {
fmt.Println(B.NewB())
}
c/go.mod
:
module example.com/c
go 1.17
replace example.com/b => ../b
require example.com/b v0.0.0-00010101000000-000000000000 // indirect
在c
目录中运行go run main.go
时,我得到以下错误:
../b/b.go:3:8: 缺少提供包 example.com/a 的 go.sum 条目(由 example.com/b 导入);要添加:
go get example.com/b@v0.0.0-00010101000000-000000000000
而go get example.com/b@v0.0.0-00010101000000-000000000000
显示:
go: 正在下载 example.com/a v0.0.0-00010101000000-000000000000
example.com/b 导入
example.com/a: 无法识别的导入路径 "example.com/a":读取 https://example.com/a?go-get=1 时出错:404 Not Found
确实,这是一个本地包,不可在互联网上获取,并且在所有必要的go.mod
文件中使用了replace
。
如何使用本地包?
如果我将example.com
重命名为example
,我会得到missing dot in first path element
错误。
英文:
I have the following files :
$GOPATH/src/example.com
├── a
│ ├── a.go
│ └── go.mod
├── b
│ ├── b.go
│ └── go.mod
└── c
├── go.mod
└── main.go
Contents are :
a/a.go
:
package a
func NewA() int {
return 1
}
a/go.mod
:
module example.com/a
go 1.17
b/b.go
:
package b
import A "example.com/a"
func NewB() int {
return A.NewA()
}
b/go.mod
:
module example.com/b
go 1.17
replace example.com/a => ../a
require example.com/a v0.0.0-00010101000000-000000000000 // indirect
c/main.go
:
package main
import "fmt"
import B "example.com/b"
func main() {
fmt.Println(B.NewB())
}
c/go.mod
:
module example.com/c
go 1.17
replace example.com/b => ../b
require example.com/b v0.0.0-00010101000000-000000000000 // indirect
When go run main.go
in the c
directory I get :
../b/b.go:3:8: missing go.sum entry for module providing package example.com/a (imported by example.com/b); to add:
go get example.com/b@v0.0.0-00010101000000-000000000000
And go get example.com/b@v0.0.0-00010101000000-000000000000
says :
go: downloading example.com/a v0.0.0-00010101000000-000000000000
example.com/b imports
example.com/a: unrecognized import path "example.com/a": reading https://example.com/a?go-get=1: 404 Not Found
For sure this is a local package and not available on internet, and it use replace
in all necessary go.mod
files.
How to work with local packages?
If I rename example.com
to example
I get : missing dot in first path element
.
答案1
得分: 3
> replace
指令仅适用于主模块的go.mod
文件,在其他模块中将被忽略。有关详细信息,请参阅最小版本选择。
在构建主包/模块时,b/go.mod
中的replace
指令没有任何效果。您必须将该replace
指令添加到主模块的go.mod
中。
因此,请将其添加到c/go.mod
中。在c
文件夹中运行go mod tidy
后,它将如下所示:
module example.com/c
go 1.17
replace example.com/b => ../b
replace example.com/a => ../a
require example.com/b v0.0.0-00010101000000-000000000000
require example.com/a v0.0.0-00010101000000-000000000000 // indirect
如果在c/go.mod
中没有这个replace
指令,您会看到错误消息,因为go工具尝试从example.com
(这是一个现有的域)获取包,但它不包含go模块。
英文:
Quoting from Go Modules Reference: replace
directive:
> replace
directives only apply in the main module’s go.mod
file and are ignored in other modules. See Minimal version selection for details.
The replace
directive in b/go.mod
has no effect when you build the main package / module. You must add that replace
directive to the main module's go.mod
.
So add it to c/go.mod
. After running go mod tidy
in the c
folder, it'll look like this:
module example.com/c
go 1.17
replace example.com/b => ../b
replace example.com/a => ../a
require example.com/b v0.0.0-00010101000000-000000000000
require example.com/a v0.0.0-00010101000000-000000000000 // indirect
Without this replace
in c/go.mod
you were seeing the error message because the go tool tried to get the package from example.com
(which is an existing domain) but it does not contain a go module.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论