英文:
Conflict in repo name and module name
问题
我创建了一个名为qjson/qjson-go
的github.com
项目,其中包含了名为qjson
的包,你可以在这里看到。我之所以这样命名github项目,是因为github.com/qjson/
包含了其他不同语言的项目(例如qjson-c
)。
不幸的是,当我尝试将该项目导入为github.com/qjson/qjson-go
时,我遇到了以下错误:
$ go mod tidy
go: finding module for package github.com/qjson/qjson-go
go: downloading github.com/qjson/qjson-go v0.0.0-20210128102242-170c47e2db46
github.com/xxx/xxx imports
github.com/qjson/qjson-go: module github.com/qjson/qjson-go@latest found (v0.0.0-20210128102242-170c47e2db46), but does not contain package github.com/qjson/qjson-go
显然我做错了。我理解由于导入语句,我们应该使用gjson-go
作为包标识符。
我应该怎么做才能使git项目的名称为qjson-go
,包的名称为qjson
?
我猜想一种解决方案是在qjson-go
内创建一个名为qjson
的子目录,并将所有包文件移动到其中。然后用户可以使用import "github.com/qson/qson-go/qjson"
。这样正确吗?还有其他避免重复的解决方案吗?
英文:
I created the github.com
project qjson/qjson-go
that contains the package name qjson
that you can see here. I named the github project this way because github.com/qjson/
contains other projects for different languages (e.g. qjson-c
).
Unfortunately, I get the following error when I try to import the project as github.com/qjson/qjson-go
:
$ go mod tidy
go: finding module for package github.com/qjson/qjson-go
go: downloading github.com/qjson/qjson-go v0.0.0-20210128102242-170c47e2db46
github.com/xxx/xxx imports
github.com/qjson/qjson-go: module github.com/qjson/qjson-go@latest found (v0.0.0-20210128102242-170c47e2db46), but does not contain package github.com/qjson/qjson-go
I’m apparently doing it wrong. I understand that due to the import statement we are then expected to use gjson-go
as package identifier.
What must I do so that the git project can be named qjson-go
and the package qjson
?
I assume that one solution is to create a sub-directory named qjson
inside qjson-go
and move all package files in it. The user would then import "github.com/qson/qson-go/qjson"
. Is that correct ? Is there another solution avoiding the stutter ?
答案1
得分: 1
这个程序按预期工作:
package main
import (
"fmt"
"github.com/qjson/qjson-go/qjson"
)
func main() {
fmt.Println(qjson.ErrDivisionByZero)
}
问题在于你正在使用这个文件结构:
qjson/engine.go
qjson/errors.go
而你应该将它们放在顶层,像这样:
engine.go
errors.go
所以你可以修复目录并标记一个新版本,或者只是保持文件不变,并更改你的导入以匹配我上面的示例。
英文:
This program works as expected:
package main
import (
"fmt"
"github.com/qjson/qjson-go/qjson"
)
func main() {
fmt.Println(qjson.ErrDivisionByZero)
}
The issue is that you are using this file structure:
qjson/engine.go
qjson/errors.go
When you should just be putting them at the top level, like this:
engine.go
errors.go
So you can either fix the directory, and tag a new version, or just leave the
files as is, and change your imports to match what I have above.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论