How to import and build with a group of local modules in Go

huangapple go评论82阅读模式
英文:

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

引用自Go Modules参考:replace指令:

> 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.

huangapple
  • 本文由 发表于 2021年11月12日 20:27:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/69942954.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定