Go更喜欢从远程仓库获取还是更喜欢使用本地文件?

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

Does Go prefer to fetch from remote repository or prefer to use local files?

问题

假设我的整个代码库同时存在于github.com/my_repo和本地文件系统中。一个源文件导入了"github.com/my_repo/pkg"。当我运行go run时,go会获取远程文件并导入它,还是更倾向于使用本地文件?

如果存在一个通用的远程代码库(不是github/launchpad等),go会更倾向于获取远程代码库还是使用本地文件?

英文:

Let's assume that my entire repository is at both github.com/my_repo and local file system. A source file imports "github.com/my_repo/pkg". When I run go run, does go fetch the remote file and import it, or does it prefer local files?

What if there is a generic remote repository (not github/launchpad/etc), will go prefer to fetch remote repository or prefer to use local files?

答案1

得分: 2

'go run'从不从网络获取任何内容。唯一能够这样做的Go命令是'go get'。

英文:

'go run' never fetches anything from the net. The only Go command which does that is 'go get'.

答案2

得分: 1

根据http://golang.org/doc/code.html#remote

> 如果指定的包在工作区中不存在,go get将把它放在GOPATH指定的第一个工作区中。(如果包已经存在,go get将跳过远程获取并与go install行为相同。)

换句话说:

  1. Go将在你的$GOPATH中检查包是否存在
  2. 如果不存在,Go将调用go get并获取它。

这值得注意,如果你期望获取特定版本的存储库:go将获取安装的Go版本的最新版本。如果存储库没有特定的标签,它将获取主分支的最新版本。通常在版本控制/文档中记录所需存储库的版本是一个好主意,以确保不会获取到更新的(可能会破坏的)存储库。

英文:

As per http://golang.org/doc/code.html#remote

> If the specified package is not present in a workspace, go get will place it inside the first workspace specified by GOPATH. (If the package does already exist, go get skips the remote fetch and behaves the same as go install.)

Or, in other words:

  1. Go will check for the package locally in your $GOPATH
  2. If it doesn't exist, Go will call go get and fetch it.

This is worth noting if you are expecting a specific version of a repo: go will grab the latest for the version of Go installed. If the repo doesn't have specific tags, it will grab the head from the master branch. It's generally a good idea to note the version of the repo you need somewhere in your version control/documentation to ensure you don't get a later (and potentially breaking) repo.

答案3

得分: 0

请注意,go get将获取master的HEAD...并在分离的HEAD状态下克隆您的存储库。
这意味着:如果您想添加一些提交,您需要在“无分支”模式下进行。
在进行任何本地提交之前,您需要首先执行以下操作:

git checkout -b master --track origin/master

Git 1.2(2013年第4季度)将更改此行为:请参阅Go tip(2013-09-07)中发生的事情

用于git存储库的go get

> 相关CL:CL 12923043,还有一些我不知道编号的。
>
> 如果您曾经在git存储库上使用过go get,并且稍后去查看实际的克隆,您可能会注意到HEAD处于分离状态,并且每次go get -u都会将其放回分离状态,其中HEAD指向远程分支上的特定提交。
>
> 虽然这在技术上并不是一个问题(您仍然可以检出主分支),但当不知道这个事实时,它会导致混淆和偶尔的数据丢失。
>
> 然而,在Go 1.2中,go get将始终创建一个“正确的”(即预期的)克隆,具有活动的master分支,并且go get -u将使用git pull及其所有效果,例如在冲突或未提交更改的情况下中止。
在Go 1.2之前的旧克隆将通过go get -u自动更新为新格式。

英文:

Note that go get will grab the HEAD of master... and clone your repo in a detached HEAD state.
That means: if you want to add a few commits, you would do so in "no branch" mode.
You need first, before making any local commit of your own, to:

git checkout -b master --track origin/master

Git 1.2 (Q4 2013) will change that: see What's happening in Go tip (2013-09-07):

go get for git repositories

> Relevant CLs: CL 12923043, some more I unfortunately do not have the numbers of.
>
> If you ever used go get on a git repository and later went to look at the actual clone, you might have noticed that HEAD was in a detached state, and that every go get -u would put it back in detached state, with HEAD pointing at a specific commit on a remote branch.
>
> And even though that isn’t technically a problem (you could still just check out the master branch), it would lead to confusion and occasional loss of data when one wasn’t aware of the fact.
>
> In Go 1.2, however, go get will always create a “proper” (as in expected) clone, with an active master branch, and go get -u will use git pull and all its effects, such as aborting in case of conflicts or uncommitted changes.
Old clones from before Go 1.2 will automatically be updated to the new format by go get -u.

huangapple
  • 本文由 发表于 2013年6月10日 07:37:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/17015229.html
匿名

发表评论

匿名网友

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

确定