英文:
Replacing inconvenient package name. Got error: replacement module without version must be directory path (rooted or starting with ./ or ../)
问题
问题
在go.mod文件中,我写了以下内容:
module github.com/Siiir/vector
go 1.17
require github.com/huandu/go-clone v1.3.2 // indirect
replace clone => github.com/huandu/go-clone[v1.3.2]
它说我不能进行这样的替换。
实际上,我通过导入包的名称解决了我的问题。
它很方便,而且在没有破折号的情况下工作正常。我发现我可以使用clone.something
来引用一个函数。
不需要输入go-clone.something
。
无论如何,假设一个包的名称确实很奇怪或不方便。我该如何替换它?
我看到的:
我尝试过:
- 使用终端工作:
- go mod edit -replace=clone=github.com/huandu/go-clone
得到:go: -replace=clone=github: unversioned new path must be local directory
- go mod edit -replace=clone=github.com/huandu/go-clone
- 手动编辑:
- 尝试像这样的替换:replace clone => github.com/huandu/go-clone[v1.3.2]
得到:replacement module without version must be directory path (rooted or starting with ./ or ../)
- 尝试像这样的替换:replace clone => github.com/huandu/go-clone[v1.3.2]
英文:
Problem
In go.mod file I wrote:
module github.com/Siiir/vector
go 1.17
require github.com/huandu/go-clone v1.3.2 // indirect
replace clone => github.com/huandu/go-clone[v1.3.2]
It says that I cannot do such a replacement.
I actually solved my problem with the name of imported package.
It is convenient & working without that dash. I found that I can use clone.something
to refer to a function.
No need to type go-clone.something
.
Anyway, assume that a package name is indeed crazy or inconvenient. How can I replace it?
What I've seen:
- I've seen a sibling question:
https://stackoverflow.com/questions/55533971/go-modules-replace-does-not-work-replacement-module-without-version-must-be
What I tried:
- Working with terminal:
- go mod edit -replace=clone=github.com/huandu/go-clone
got:go: -replace=clone=github: unversioned new path must be local directory
- manual editing:
- Attempts like: replace clone => github.com/huandu/go-clone[v1.3.2]
got:replacement module without version must be directory path (rooted or starting with ./ or ../)
答案1
得分: 1
无论如何,假设一个包名确实很疯狂或不方便。我该如何替换它?
你不能替换它。
而且你也不应该这样做。导入路径是你在import
声明中只写一次的内容,包名可以在每个文件级别上通过import nicename "something.you.think/is-totally/inconvenient/and/unacceptable-to/your_taste"
进行更改。
英文:
> Anyway, assume that a package name is indeed crazy or inconvenient. How can I replace it?
You cannot.
And you should not. The import path is something you write just once in the import
declaration and the package name can be changed on a per file level with import nicename "something.you.think/is-totally/inconvenient/and/unacceptable-to/your_taste"
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论