Golang导入路径最佳做法 [英] Golang import path best practice

查看:68
本文介绍了Golang导入路径最佳做法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用Golang进行私人项目(这是我的新手).

I am currently working on a private project using Golang (and I am new to it).

但是我有一个问题,我不知道为本地软件包定义导入路径的正确方法是什么.

But I have the problem that I don't know what is the proper way to define the import path for local packages.

通过此链接,我与作者有相同的感受https://medium.com/@c9s/golang-the-annoying-remote-import-path-c6c7e76517e5

I have the same feeling as the author from this link https://medium.com/@c9s/golang-the-annoying-remote-import-path-c6c7e76517e5

简而言之,如果我在Github中托管项目 foo .使用 github.com/levin/foo 作为导入路径而不只是 foo 感到奇怪.如果将代码移至Bitbucket或在AWS中托管自己的git服务器,是否会导致大量返工?

In short, if I host a project foo in Github. I feel strange to use github.com/levin/foo as import path instead of just foo. Wouldn't it cause much rework if I move my codes to Bitbucket or I host my own git server in AWS?

更糟糕的是,我可以重写我的代码来更改导入路径,但是人们在我的代码中使用的人将如何通知回购更改?我觉得没有必要要求其他人更新他们的代码.

And worse, I can rewrite my code to change the import path, but how would people using my code in theirs notify the change of repo? And I feel no sense to ask others to update their codes.

我是Golang的新手,所以随时可以说您的问题甚至无效"之类的话.

I am new to Golang so feel free to say something like "your question is not even valid".

推荐答案

您的问题的答案:

我不知道为本地软件包定义导入路径的正确方法是什么.

I don't know what is the proper way to define the import path for local packages.

如@JimB所说:

如果要使用Go工具,则需要遵循约定.如果您想上班,则需要用全名导入它,否则将其放置在GOPATH中的任何位置

If you want to use the Go tools you need to follow the conventions. If you want go get to work, then you need to import it with the full name, otherwise put it wherever you want in GOPATH

因此,如果您想使 go 正常工作,则需要使用完整的导入路径 github.com/levin/foo ,如果您要使用 希望其他人使用您的包裹.

So you need to use the complete import path github.com/levin/foo if you want go get to work, and you should do it this way if you expect other people to use your package.

第二个问题的答案:

如果我将代码移至Bitbucket或在AWS中托管自己的git服务器,是否会造成大量返工?

Wouldn't it cause much rework if I move my codes to Bitbucket or I host my own git server in AWS?

有一种方法可以使用自定义域名作为导入路径,并且仍在需要的地方托管代码,这被称为虚荣导入路径.您只需向使用的导入文件中提供的html文件中添加一些元标记即可.

There is a way to use a custom domain name as import path and still host your code wherever you want, it's called a vanity import path I think. You just need to add some meta tags to the html file that gets served in the import file that you use.

本文对此进行了说明,但总的来说,访问自定义导入路径时在自定义域中提供的文件,您需要添加一个 go-import 元标记,该标记指向您托管代码的位置.本文使用 github.com/foo/bar 作为托管代码的位置的示例,并使用 foo.com/bar 作为真正的导入路径.

This article explains how to do it, but in summary, in the file that gets served in your custom domain when the custom import path is accessed, you need to add a go-import meta tag that points to where you hosted the code. The article uses github.com/foo/bar as example of where you are hosting your code and foo.com/bar as your real import path.

因此,在访问 foo.com/bar 时提供服务的文件中,应该有一个像这样的元标记:

So in the file that gets served when accessing foo.com/bar there should be a meta tag like this:

<meta name="go-import" content="golang.org/x/tools git http://github.com/foo/bar">

您将继续在github中托管代码.然后,如果将托管位置更改为代码,则只需更改meta标记的值,但是使用该软件包的所有代码将继续使用完全相同的导入路径"foo.com/bar .

And you continue to host your code in github. Then if you change the hosting place to your code you just change the value of the meta tag, but all the code that uses the package continue to use the exact same import path "foo.com/bar.

唯一的问题是,现在您的项目可以通过2种不同的路径导入: foo.com/bar github.com/foo/bar .要解决此问题,他们有规范导入,该规范仅允许使用自定义路径导入软件包而不是github,它只是在每个软件包文件的软件包名称旁边添加注释:

The only issue with this is that now your project can get imported by 2 different paths: foo.com/bar and github.com/foo/bar. To fix this they have canonical imports which only allow the package to be imported using the custom path and not the github one, it's only adding a comment next to the package name on each package file:

package bar // import "foo.com/bar"

这是避免出现问题的唯一方法.如果您正在使用仅在本地使用的软件包,则可以使用@srxf答案,只要知道go工具不会与该代码一起使用即可(例如 go get ).如果您希望代码按预期工作,那么这就是方法.

This is the only way to avoid the issue you have. You can use @srxf answer if you are using a package that is just going to be used locally just know that the go tools are not going to work with that code (like go get). If you want the code to work as it is intended then this is the way to go.

作为评论,我知道为本地软件包导入 github.com/levin/foo 感觉很愚蠢,但是如果您在另一个软件包中使用该软件包(比如说 foo2 ),然后其他人导入了 foo2 ,这使编译器可以确切地知道从何处获取 foo2 的依赖项,因为代码中的所有导入都包括整个路径,而不是本地文件的名称.这样,人们始终可以获取他们需要的软件包依赖关系,而不必在存储库中包含这些文件,也无需进行任何配置即可使用.这不是完美的方法,但这是可行的方法,他们称其为通过配置进行约定"或类似的方法.

As a comment, I know it feels silly importing github.com/levin/foo for a local package, but if you use that package in another package (say foo2) and someone else imports foo2, this allows the compiler to know exactly where to get the dependencies for foo2, because all the import in the code include the whole route, not the name of a local file. This way people can always get the dependencies they need for your package without having to include those files in your repo, and without having to configure anything for it to work. It's not perfect but it's the way go works, they call it convention over configuration or something like that.

这篇关于Golang导入路径最佳做法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆