英文:
What's the correct organisation for Go projects using Git (or any VCS)?
问题
我的GOPATH是/Users/joe/go
。我正在一个名为myproj
的项目上工作,它是一个名为myproj
的包。
如果我想要写import "myproj"
,那么我需要的目录结构是:
$GOPATH/src/myproj/myproj.go
$GOPATH/src/myproj/myproj_test.go
...
然而,我似乎无法将其与Git配合使用。如果我查看Google的示例包,我会看到以下格式:
go.example/hello/hello.go
go.example/LICENSE
其中go.example
是仓库的名称。
因此,实际的包目录在仓库的内部找到。如果我将此仓库的内容放在$GOPATH的一个目录中,例如
$GOPATH/src/go.example/hello/hello.go
$GOPATH/src/go.example/LICENSE
那么我将不得不输入import "go.example/hello"
而不是import "hello"
。
回到我的项目,我需要将其打包到一个Git仓库中,然后我需要一个容器目录。所以我的当前文件结构是:
$GOPATH/src/myproj # 用于git仓库的目录
$GOPATH/src/myproj/.git
$GOPATH/src/myproj/LICENSE # 仓库根目录中的文件
$GOPATH/src/myproj/myproj/myproj.go # 包目录中的包文件
$GOPATH/src/myproj/myproj/myproj_test.go
我需要外部的myproj
目录来限定git仓库,我需要内部的目录来作为包目录。结果是,我需要输入import "myproj/myproj"
而不是import "myproj"
。
我该如何解决这个问题?我需要为每个正在开发的项目添加多个GOPATH吗?
提前感谢。
英文:
My GOPATH is /Users/joe/go
. I am working on a project called myproj
, which is a package called myproj
.
If I want to be able to write import "myproj"
then the directory structure I need is:
$GOPATH/src/myproj/myproj.go
$GOPATH/src/myproj/myproj_test.go
...
However, I can't seem to make this fit with Git. If I look at an example package from Google I see the following format:
go.example/hello/hello.go
go.example/LICENSE
Where go.example
is the name of the repo.
So the actual package directories are found inside the repository. If I put the contents of this repository in a directory on my $GOPATH, e.g.
$GOPATH/src/go.example/hello/hello.go
$GOPATH/src/go.example/LICENSE
then I will have to type import "go.example/hello"
rather than import "hello"
.
Coming back to my project, I need to package this up in a Git repository then I need a container directory. So my current file structure is:
$GOPATH/src/myproj # The dir for the git repo
$GOPATH/src/myproj/.git
$GOPATH/src/myproj/LICENSE # Files in the base of the repo
$GOPATH/src/myproj/myproj/myproj.go # Package files in package dir
$GOPATH/src/myproj/myproj/myproj_test.go
I need the outer myproj
directory to bound the git repository and I need the inner one to be the package directory. The upshot is that I need to type import "myproj/myproj"
rather than import "myproj"
.
How do I fix this? Do I have to add multiple $GOPATHS, one for each project I'm developing?
Thanks in advance.
答案1
得分: 8
首先:
> 我需要为每个我正在开发的项目添加多个$GOPATH
吗?
不需要,你不需要多个$GOPATH
。
它们是用来管理你的(可能有多个)项目的工具。当你设置一个新的项目环境时,你已经知道该项目可能有一些依赖项 - 可能是特定于该项目的,或者应该与其他人的设置类似,你创建一个新的项目文件夹并将其设置为GOPATH
。这样,你可以为该项目使用(检出)特定版本的库,同时为其他项目在其他项目文件夹(= GOPATH
)中使用其他版本。
至于你的路径问题:Go遵循一个通用的范例作者/项目
(或组织/项目
)。这将防止当多个人、作者和组织以相同的名称启动项目时出现命名冲突。(作者也可以在“他”的文件夹中使用子文件夹、子项目。)
如果你意识到这一点,仍然想要只使用myproj
作为你的包路径,在该文件夹中创建git仓库没有问题 - 与你链接的示例包相反。
> 回到我的项目,我需要将其打包到一个Git仓库中,然后我需要一个容器目录。
你为什么这样认为呢?Go不需要这样做。Git也不需要。
所以以下方式是可行的:
/src/myproj/.git
/src/myproj/myproj.go
虽然这不是推荐的做法,你可以将仓库放在你的myproj
文件夹中。
我按照以下方式进行了测试:
FOLDER
FOLDER/src
FOLDER/src/myproj
FOLDER/src/myproj/myproj.go
FOLDER/src/mainproj
FOLDER/src/mainproj/main.go
在folder/src/myproj/myproj.go
中:
package myproj
type My struct {
I int
}
在folder/src/mainproj/main.go
中:
package main
import (
"fmt"
"myproj"
)
func main() {
my := myproj.My{7}
fmt.Printf("Works! %v", my.I)
}
运行
cd FOLDER
set GOPATH=FOLDER
go run src/mainproj/main.go
将输出:
Works! 7
现在,如果你在文件夹FOLDER/src/myproj
中执行git init
,这对Go本身没有任何影响。
英文:
First off:
> Do I have to add multiple $GOPATH
s, one for each project I'm developing?
No, you don’t need multiple $GOPATH
s at all.
They are a tool for managing your (potentially several) projects. When you set up a new project environment which you already know will have some dependencies - potentially unique to that project, or which should be similiar to other peoples setups, you create a new project folder and set it as the GOPATH
. That way, you can also use (= check out) specific versions of a library for that project, while using other versions for your other projects in other project folders (= GOPATH
s).
As for your path issue: Go follows a generic paradigm of author/project
(or organization/project
). This will prevent naming clashes when several people, authors and organizations, start projects with the same names. (The author may then use sub-folders, sub-projects in “his” folder as well ofc.)
If you are aware of this and still want to use only myproj
as your package path, there is no problem in creating the git repository in that folder - in contrary to the example package you linked to.
> Coming back to my project, I need to package this up in a Git repository then I need a container directory.
What makes you think so then? Go does not need it. Git does neither.
So the following will work:
/src/myproj/.git
/src/myproj/myproj.go
While it is not the encouraged practice, you can put the repository into your myproj
folder.
I tested this as follows:
FOLDER
FOLDER/src
FOLDER/src/myproj
FOLDER/src/myproj/myproj.go
FOLDER/src/mainproj
FOLDER/src/mainproj/main.go
With folder/src/myproj/myproj.go
package myproj
type My struct {
I int
}
and folder/src/mainproj/main.go
package main
import (
"fmt"
"myproj"
)
func main() {
my := myproj.My{7}
fmt.Printf("Works! %v", my.I)
}
Running
cd FOLDER
set GOPATH=FOLDER
go run src/mainproj/main.go
will output:
Works! 7
Now, if you git init
in the folder FOLDER/src/myproj
, that does not matter to Go itself at all.
答案2
得分: 2
通常的设置如下:
$GOPATH,初步只需要一个路径,你的例子/Users/joe/go/
很好。现在你有一个在http://github.com/joe/myproj
上可见的github仓库myproj
。这个包的导入语句应该是
import "github.com/joe/myproj"
回顾一下你的(单值)$GOPATH,go工具将在$GOPATH/src/github.com/joe/myproj
中寻找你的包,这是你的“外部”github仓库的“本地”根目录,里面有myproj.go、etc.go等文件。
如果没有这些约定,就会与Jack(以及Alice和Bob)的项目发生命名空间冲突,意外地也叫做myproj
,所以我建议从一开始就习惯这种方式。
英文:
The usual setup goes like this:
$GOPATH, in the first approximation needs only one path, your example /Users/joe/go/
is just fine. Now you have a github repository myproj
seen at http://github.com/joe/myproj
. The import statement for this package should be
import "github.com/joe/myproj"
Looking back at your (single valued) $GOPATH, the go tool will look for your package at $GOPATH/src/github.com/joe/myproj
and that's the "local" root of your "external" github repository with files myproj.go, etc.go, ... inside.
Without these conventions there will be namespace clashes with Jack's (and Alice's and Bob's) project(s) accidentally called also myproj
, so I recommend to get used to this right from the beginning.
答案3
得分: 0
你会自然地导入多个包:
- 标准包
- 一些你使用
go get
下载的包 - 你项目中的一些包
- 其他项目中的一些包
最简单的方法是选择一个目录,在这个目录中使用go get
获取所有外部包(例如mysql驱动程序),并将你的项目目录添加到GOPATH中。
例如,这是我的(简化后的)GOPATH:
export GOPATH=/home/dys/dev/go:/home/dys/dev/Chrall/go:/home/dys/dev/braldop/go:/home/dys/dev/lg/go
我将所有使用go get
获取的外部库放在/home/dys/dev/go
中,Chrall、braldop和lg是我的三个项目。
在这三个项目中,我有包和命令。例如:
/home/dys/dev/lg/go/src/pkg1/xxx.go
/home/dys/dev/lg/go/src/pkg2/xxx.go
/home/dys/dev/lg/go/src/prog1/xxx.go
等等。
所有的包都可以通过GOPATH找到。
例如,当我在其中一个xxx.go中使用mysql驱动程序时:
import (
"database/sql"
_ "github.com/ziutek/mymysql/godrv"
)
该驱动程序已经通过以下方式安装在/home/dys/dev/go中:
go get github.com/ziutek/mymysql/godrv
参考资料: http://golang.org/doc/code.html
> GOPATH=/home/user/ext:/home/user/mygo
>
> (在Windows系统上,使用分号作为路径分隔符,而不是冒号。)
>
> 列表中的每个路径(在本例中为/home/user/ext或/home/user/mygo)指定了工作区的位置。工作区包含Go源文件及其关联的包对象和命令可执行文件。
英文:
You'll naturally import multiple packages :
- the standard ones
- a few ones you'll download using
go get
- a few ones in your project
- a few ones in other projects
The simplest is to choose a directory where you'll go get
all the external packages (mysql driver for example) and add your project directories to GOPATH.
For example, here's my (simplified) GOPATH :
export GOPATH=/home/dys/dev/go:/home/dys/dev/Chrall/go:/home/dys/dev/braldop/go:/home/dys/dev/lg/go
I put all the external libraries obtained using go get
in /home/dys/dev/go
and Chrall, braldop and lg are 3 of my projects.
In each one of those 3 projects, I have packages and commands. For example :
/home/dys/dev/lg/go/src/pkg1/xxx.go
/home/dys/dev/lg/go/src/pkg2/xxx.go
/home/dys/dev/lg/go/src/prog1/xxx.go
etc.
All packages are found via GOPATH.
For example when I use a driver mysql, in one of those xxx.go :
import (
"database/sql"
_ "github.com/ziutek/mymysql/godrv"
)
The driver has been installed in /home/dys/dev/go using
go get github.com/ziutek/mymysql/godrv
Reference : http://golang.org/doc/code.html
> GOPATH=/home/user/ext:/home/user/mygo
>
> (On a Windows system use
> semicolons as the path separator instead of colons.)
>
> Each path in the list (in this case /home/user/ext or /home/user/mygo)
> specifies the location of a workspace. A workspace contains Go source
> files and their associated package objects, and command executables.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论