git 去,去得到,去安装,本地包和版本控制
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10130341/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Go, go get, go install, local packages, and version control
提问by Seth Hoenig
I am having trouble understanding the workflow for creating a go project that has local packages.
我无法理解创建具有本地包的 go 项目的工作流程。
Say I create a new project, using git for version control, which has a main.go file and a tools.go file which will be in package utils. So I have a directory structure such as this:
假设我创建了一个新项目,使用 git 进行版本控制,它有一个 main.go 文件和一个将在包 utils 中的 tools.go 文件。所以我有一个这样的目录结构:
/myproject/
main.go
utils/
tools.go
main.go looks like this:
main.go 看起来像这样:
package main
import "./utils"
func main() {
utils.DoSomthing()
}
and tools.go looks like this:
和 tools.go 看起来像这样:
package utils;
func DoSomething() {
}
Everything works just fine locally, using go buildand go run. But this is being hosted on github, and I'd like to be able to have others use the go getcommand to install it. So the local package import must be changed to use the format "github.com/user/project/utils", which works, except now I have two copies of the source code, and the real problem is that the copy with the git history has an import that makes use of the downloaded copy. So if I'm working on the copy with the git history, any changes made to tools.go will go unnoticed, because it will be using the downloaded copy.
使用go build和go run在本地一切正常。但是这是在 github 上托管的,我希望能够让其他人使用go get命令来安装它。所以本地包导入必须改成使用格式“github.com/user/project/utils”,这样就可以了,除了现在我有两份源代码,真正的问题是带有git历史的副本有一个使用下载副本的导入。因此,如果我正在处理带有 git 历史记录的副本,则对 tools.go 所做的任何更改都不会被注意到,因为它将使用下载的副本。
So I'm wondering if someone can explain the right way of using go get, version control, and package imports within the same project.
所以我想知道是否有人可以解释在同一项目中使用go get、版本控制和包导入的正确方法。
回答by tux21b
I've just written a short step-by-step guide on how I am using the new go tooland github.com. You might find it useful:
我刚刚写了一个简短的分步指南,介绍我如何使用新的go 工具和github.com。您可能会发现它很有用:
1. Setup your GOPATH
1. 设置你的 GOPATH
You can set the environment variable GOPATH
to any directory you like. If you have larger projects, it's probably a good idea to create a different GOPATH for each of them. I would recommend this approach especially for the deployment, so that updating a library for project A doesn't break project B which might require an earlier version of the very same library.
您可以将环境变量设置为GOPATH
您喜欢的任何目录。如果您有较大的项目,为每个项目创建不同的 GOPATH 可能是个好主意。我会特别推荐这种方法用于部署,以便更新项目 A 的库不会破坏项目 B,这可能需要相同库的早期版本。
Also note that you can set your GOPATH to a list of directories, delimited by colons. So you might have a GOPATH containing all commonly used packages, and separate GOPATHS for each project with additonal packages or different versions of existing packages.
另请注意,您可以将 GOPATH 设置为以冒号分隔的目录列表。所以你可能有一个包含所有常用包的 GOPATH,并为每个项目单独的 GOPATHS 与附加包或现有包的不同版本。
But unless your are working on a lot of different Go projects simultaneously, its probably enough to have just a single GOPATH locally. So, let's create one:
但是除非您同时处理许多不同的 Go 项目,否则在本地只有一个 GOPATH 可能就足够了。所以,让我们创建一个:
mkdir $HOME/gopath
Then you need to set two environment variables to tell the go toolwhere it can find existing Go packages and where it should install new ones. It's probably best to add the following two lines to your ~/.bashrc
or ~/.profile
(and do not forget to reload your .bashrc afterwards).
然后你需要设置两个环境变量来告诉go 工具它可以在哪里找到现有的 Go 包以及它应该在哪里安装新的包。最好将以下两行添加到您的~/.bashrc
or ~/.profile
(并且不要忘记之后重新加载您的 .bashrc )。
export GOPATH="$HOME/gopath"
export PATH="$GOPATH/bin:$PATH"
2. Create a new project
2.新建项目
If you want to create a new Go project which should be hosted at github.comlater, you should create this project under $GOPATH/src/github.com/myname/myproject
. It's important that the path matches the URL of the github.com repo, because the go tool will follow the same convention. So, let's create the project root and initialize a new git repository there:
如果你想创建一个新的 Go 项目,稍后应该在github.com上托管,你应该在$GOPATH/src/github.com/myname/myproject
. 路径与 github.com 存储库的 URL 匹配很重要,因为 go 工具将遵循相同的约定。因此,让我们创建项目根目录并在那里初始化一个新的 git 存储库:
mkdir -p $GOPATH/src/github.com/myname/myproject
cd $GOPATH/src/github.com/myname/myproject
git init
Because I do not like to type such long paths, I normally create symbolic links for the projects I am currently working on in my home folder:
因为我不喜欢输入这么长的路径,所以我通常在我的主文件夹中为我目前正在处理的项目创建符号链接:
ln -s $GOPATH/src/github.com/myname/myproject ~/myproject
3. Write your application
3. 编写您的应用程序
Start coding and don't forget to git add
and git commit
your files. Also, do not use relative imports like import "./utils"
for sub-packages. They are currently undocumented and shouldn't be used at all, because they won't work with the go tool. Use imports like github.com/myname/myproject/utils
instead.
开始编码,不要忘记git add
和git commit
您的文件。另外,不要像import "./utils"
子包那样使用相对导入。它们目前没有记录,根本不应该使用,因为它们不能与 go 工具一起使用。github.com/myname/myproject/utils
改为使用导入。
4. Publish your project
4. 发布你的项目
Create a new repository at github.com, upload your SSH public key if you haven't done that before and push your changes to the remote repository:
在 github.com创建一个新的存储库,如果您之前没有上传过您的 SSH 公钥,然后将您的更改推送到远程存储库:
git remote add origin [email protected]:myname/myproject.git
git push origin master
5. Continue working on your project
5. 继续你的项目
If you have set the GOPATH in your .bashrc and if you have created a symlink to your project in your home folder, you can just type cd myproject/
and edit some files there. Afterwards, you can commit the changes using git commit -a
and send them to github.comby doing a git push
.
如果您在 .bashrc 中设置了 GOPATH 并且在您的主文件夹中创建了指向您的项目的符号链接,您只需cd myproject/
在那里键入和编辑一些文件。之后,您可以使用 提交更改git commit -a
并将它们发送到github.com通过执行git push
.
回答by Sonia
You probably don't want two copies of the source. Following How to Write Go Code, you should have a path where you do your Go development, lets say "godev", and under that, a "src" directory, and under that, your "github.com/user/project" and "github.com/user/project/utils". (I agree, it seems a little rigid, but the advantage explained to us is freedom from make files.) Ditch the myproject, this is where you will do your work.
您可能不想要源的两个副本。遵循How to Write Go Code,您应该有一个进行 Go 开发的路径,比如说“godev”,在该路径下,有一个“src”目录,在该目录下,您的“github.com/user/project”和“github.com/user/project/utils”。(我同意,这似乎有点僵硬,但向我们解释的优点是免于生成文件。)放弃 myproject,这是您工作的地方。
You will have GOPATH set to godev at least, but you will probably want your GOPATH to start with a path for external packages that are not yours. For example the GOPATH I use is <my place on the file system>/goext:<my place on the file system>/godev
.
您至少将 GOPATH 设置为 godev,但您可能希望 GOPATH 以不属于您的外部软件包的路径开头。例如我使用的 GOPATH 是<my place on the file system>/goext:<my place on the file system>/godev
.
You are right that your import in main.go should now read "github.com/user/project/utils.
你是对的,你在 main.go 中的导入现在应该是“github.com/user/project/utils。
Don't worry about go get or any of the go commands overwriting your files or messing up version control. Through GOPATH, they see where you are working and they know how version control works.
不要担心 go get 或任何 go 命令会覆盖您的文件或搞乱版本控制。通过 GOPATH,他们可以看到您正在工作的地方,并且他们知道版本控制是如何工作的。
回答by Daniel YC Lin
If you want to keep your code in local version repository, just put your code in GOPATH.
如果您想将您的代码保存在本地版本存储库中,只需将您的代码放在 GOPATH 中。
GOPATH accepts multiple paths. eg. on linux
GOPATH 接受多个路径。例如。在 linux 上
GOPATH=$HOME/go:$HOME/prj/foo
So, you could go get 3rd party packages installed in $HOME/go/src/... And, you could keep your code controlled in $HOME/prj/foo/src.
因此,您可以在 $HOME/go/src/... 中安装 3rd 方软件包,并且可以在 $HOME/prj/foo/src 中控制您的代码。
ref: go help gopath
参考: go help gopath
回答by rellocs wood
lot of people say should use absolutlly path to construct project struct and import :
很多人说应该使用绝对路径来构建项目结构和导入:
import "github.com/user/utils"
but this may limit your project within a single repo (github). I would like to use relative path instead :
但这可能会将您的项目限制在单个 repo (github) 内。我想改用相对路径:
import "./utils"
I havn't find a way to do that, I raise a question here: how to oraganize GO project packages if it will hosted on different repos(github & sourceForge)
我还没有找到办法做到这一点,我在这里提出一个问题: 如果 GO 项目包将托管在不同的存储库(github 和 sourceForge)上,如何组织它
回答by Diego
After looking for this answer in different posts and Go documentation, with Go 1.11 there has been released a new approach to handle packages and is using Go Modules.
在不同的帖子和 Go 文档中寻找这个答案后,Go 1.11 发布了一种处理包的新方法,并且正在使用 Go 模块。
Read the whole post series about Go Modules here.
在此处阅读有关 Go Modules 的整个系列文章。
Go Modules also takes care of version control as well
Go Modules 还负责版本控制