一个项目的Golang工作区

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

Golang workspace for a project

问题

我有一个使用Go语言编写的项目,我将代码放在了git服务器的某个位置。
现在我的所有代码都在$GOPATH/mygitserver/folder/下。

一切都很好,直到git服务器的URL发生变化,所以我需要将所有的导入路径替换为新的git服务器URL。因此,我不想在将来再次遇到这个问题,希望改变它的工作方式。

所以,我计划将整个工作空间上传到git(排除外部库)。这样,我将直接在src文件夹下拥有一个包(例如:src/mypackage)。这样,当我改变git服务器的URL时,一切都将正常工作。

问题是:这样做(上传工作空间到git)是否是一个好的做法?或者我们还有其他选择?

还有一个之前不存在的问题。所以,在克隆我的工作空间之后,我必须为项目使用的所有库调用"go get"命令。(之前不会出现这个问题,因为当我调用"go get mygitserver/package"时,Golang会自动下载依赖项)。有没有办法通过一个命令下载所有的依赖项?

英文:

I have a golang project and I put the code somewhere in git server.
All my code are now on $GOPATH/mygitserver/folder/.

Everything is goods until the time that the gitserver is changes the url, so I need to replace all import to new gitserver url. So, i don't want to have it again in the future and want to change the way it works.

So instead of push the package only to my git server, now I am planning to upload the entire workspace to git (exclude the external library). So, I will have a package direcly to src folder (example : src/mypackage). So, when I change the gitserver url, everything will still works.

The question is : is it a good practice to do it (upload the workspace to git)? Or do we have another option?

And also it comes with another problem that not exist before. So, after I clone my workspace, I have to call "go get" for all of my library used by the project. (This not happen before because when I call go get mygitserver/package, Golang automatically download the dependency). Is there any way to make all the dependency downloaded my just one command?

答案1

得分: 3

如果你希望你的服务器经常迁移,一种可能的解决方案是使用"虚拟导入路径"。

虚拟导入路径允许你将代码保留在你拥有的域名的导入路径上,该路径会将你重定向到代码存储的位置。例如,如果你将代码托管在Bitbucket上,但担心将来可能会迁移,而你拥有域名example.net,你可以将代码托管在这样一个位置,以便你可以使用import "example.net/myproject"来导入它,而go get工具会在后台从Bitbucket获取代码。为了实现这一点,你必须在你想要的域名上提供一个包含以下形式的自定义元标签的文档:

<meta name="go-import" content="import-prefix vcs repo-root">

例如,在example.net/myproject上提供以下HTML文件将会重定向go get以克隆提供的Bitbucket URL(当然,它也可以指向任何其他Git服务,包括你自己的):

<meta name="go-import" content="example.net/myproject git https://bitbucket.org/myname/myproject.git">

要配置Nginx来提供这些重定向,可以使用以下类似的配置(当然,要将myPackage和URL替换为你的项目名称和URL):

location ~ /myPackage/[a-z][a-z0-9]* {
    if ($args = "go-get=1") {
        add_header Content-Type text/html;
        return 200 '<meta name="go-import" content="$host/myPackage git https://bitbucket.org/myName/myPackage.git">';
    }
    rewrite ^ https://mygithosting.example.org/myName/myPackage? permanent;
}
location ~ /myPackage$ {
    if ($args = "go-get=1") {
        add_header Content-Type text/html;
        return 200 '<meta name="go-import" content="$host/myPackage git https://mygithosting.example.org/myName/myPackage.git">';
    }
    rewrite ^ https://mygithosting.example.org/myName/myPackage? permanent;
}

现在,每当你迁移Git服务器时,你只需更改元标签中的URL。

如果你担心一些用户直接从你的Git服务器导入,而其他用户使用虚拟导入路径,你还可以将该路径设置为包的规范导入路径。规范导入路径是在Go程序的某个文件的package行上指定的特殊注释(无论是哪个文件都可以)。它们的格式如下:

import mypackage // import "example.com/mypackage"

现在,如果上述代码实际上托管在其他地方,并且有人尝试直接导入它,go get将会报错:

$ go get mygithosting.example.org/myName/myPackage
package mygithosting.example.org/myName/myPackage: code in directory /go/mygithosting.example.org/myName/myPackage expects import "example.com/mypackage"

英文:

If you expect your server to move frequently one possible solution is to use a vanity import path.

Vanity import paths allow you to keep your code at an import path on a domain you own which redirects you to the location where the code is stored. For instance, if you host your code on Bitbucket but are worried you might move in the future and you own the domain example.net, you could host your code such that you'd import it with import &quot;example.net/myproject&quot; and behind the scenes the go get tool would fetch it from Bitbucket. To do this, you must serve a document on the domain you want that contains a custom meta tag of the form:

&lt;meta name=&quot;go-import&quot; content=&quot;import-prefix vcs repo-root&quot;&gt;

So for instance serving the following HTML file at example.net/myproject would redirect go get to clone the provided Bitbucket URL (it could also of course point to any other Git service including your own):

&lt;meta name=&quot;go-import&quot; content=&quot;example.net/myproject git https://bitbucket.org/myname/myproject.git&quot;&gt;

To configure Nginx to serve these redirects, a configuration like the following can be used (replacing myPackage and the URLs with your projects name and URLs, of course):

location ~ /myPackage/[a-z][a-z0-9]* {
	if ($args = &quot;go-get=1&quot;) {
		add_header Content-Type text/html;
		return 200 &#39;&lt;meta name=&quot;go-import&quot; content=&quot;$host/myPackage git https://bitbucket.org/myName/myPackage.git&quot;&gt;&#39;;
	}
	rewrite ^ https://mygithosting.example.org/myName/myPackage? permanent;
}
location ~ /myPackage$ {
	if ($args = &quot;go-get=1&quot;) {
		add_header Content-Type text/html;
		return 200 &#39;&lt;meta name=&quot;go-import&quot; content=&quot;$host/myPackage git https://mygithosting.example.org/myName/myPackage.git&quot;&gt;&#39;;
	}
	rewrite ^ https://mygithosting.example.org/myName/myPackage? permanent;
}

Now any time you move your Git server, you can just change the URL in the meta tag.

If you're worried about some users importing directly from your Git server and others using the vanity import path, you can also set this path as the packages canonical import path. Canonical import paths are specified as special comments on the package line in one file of a Go program (it doesn't matter which one). They look like this:

import mypackage // import &quot;example.com/mypackage&quot;

Now if the above code is actually hosted somewhere else and someone tries to import it directly, go get will complain:

$ go get mygithosting.example.org/myName/myPackage
package mygithosting.example.org/myName/myPackage: code in directory /go/mygithosting.example.org/myName/myPackage expects import "example.com/mypackage"

huangapple
  • 本文由 发表于 2016年10月24日 08:40:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/40209254.html
匿名

发表评论

匿名网友

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

确定