Go模块中包名与GitHub路径不同的情况下,可以这样处理:

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

Go module where package name is different to github path

问题

我想将一个Go包添加到我的go.mod文件中。但是它的名称是https://github.com/user/something,而实际上源文件中的包名是somethingelse。我该如何将其添加到我的go.mod文件中?只是这样做是不起作用的:

import somethingelse github.com/user/something

我得到了一个错误:

module declares its path as: somethingelse
        but was required as: github.com/user/something
英文:

I want to add a go package to my go.mod file. But it's called e.g. https://github.com/user/something but the actual package in the source files is e.g. somethingelse. How can I add this to my go.mod file? Just doing this doesn't work:

import somethingelse github.com/user/something

I get an error:

module declares its path as: somethingelse
        but was required as: github.com/user/something

答案1

得分: 2

请将以下内容添加到您的go.mod文件中:

replace somethingelse => github.com/user/something latest

然后,按如下方式导入:

import "somethingelse"
英文:

Try adding the following to your go.mod file:

replace somethingelse => github.com/user/something latest

then, import as:

import "somethingelse"

答案2

得分: -1

如果你想将一个Go包添加到你的go mod文件中,首先使用以下命令将包下载到你的本地仓库:

go get github.com/user/something

然后在你的程序中导入该包,它将被自动导入。如果你使用的是Visual Studio Code,当你使用该特定包的语法编写代码并保存程序时,该包将自动导入。

如果没有自动导入,你可以手动导入,或者在你的IDE终端上运行以下命令:

go mod tidy

这个命令将会将go.mod文件与源文件中所需的依赖进行匹配。

  • 下载所有源文件中所需的依赖,并将其更新到go.mod文件中。
  • go.mod文件中删除源文件中不需要的所有依赖项。
英文:

If you want to add a Go package to your go mod file first download your package to your local repo using

go get github.com/user/something

Then import the package to your program it will get imported, well if you are using Visual studio code then your package will be auto imported when you write code with the syntax of that specific package and save the program.

If that doesn't import automatically, then import it manually or just run a command on your IDE terminal which is

go mod tidy

This command will basically match the go.mod file with the dependencies required in the source files.

  • Download all the dependencies that are required in your source files
    and update go.mod file with that dependency.
  • Remove all dependencies from the go.mod file which are not required
    in the source files.

huangapple
  • 本文由 发表于 2021年10月23日 16:09:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/69686257.html
匿名

发表评论

匿名网友

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

确定