仓库名称和模块名称冲突

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

Conflict in repo name and module name

问题

我创建了一个名为qjson/qjson-gogithub.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.

huangapple
  • 本文由 发表于 2021年5月24日 15:45:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/67668369.html
匿名

发表评论

匿名网友

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

确定