Replacing inconvenient package name. Got error: replacement module without version must be directory path (rooted or starting with ./ or ../)

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

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
无论如何,假设一个包的名称确实很奇怪或不方便。我该如何替换它?

我看到的:

我尝试过:

  1. 使用终端工作:
    • go mod edit -replace=clone=github.com/huandu/go-clone
      得到: go: -replace=clone=github: unversioned new path must be local directory
  2. 手动编辑:
    • 尝试像这样的替换:replace clone => github.com/huandu/go-clone[v1.3.2]
      得到: replacement module without version must be directory path (rooted or starting with ./ or ../)
英文:

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:

What I tried:

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

huangapple
  • 本文由 发表于 2022年5月12日 13:18:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/72210429.html
匿名

发表评论

匿名网友

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

确定