使用Git(或任何VCS)进行Go项目的正确组织是什么?
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.
我的GOPATH是 如果我想写导入“ myproj” code>,那么我需要的目录结构是: p>
但是,我似乎无法使它与Git相适应。 如果我查看来自Google的示例包,则会看到以下格式: p>
其中的 因此,实际的软件包目录位于存储库内 em>内。 如果我将此存储库的内容放在$ GOPATH上的目录中,例如 p>
然后我将不得不输入 回到我的项目,我需要将其打包到Git存储库中,然后需要一个容器目录。 所以我当前的文件结构是: p>
我需要外部 如何修复 这个? 我必须添加多个$ GOPATHS,针对我正在开发的每个项目添加一个吗? p>
谢谢。 p>
div> / Users / joe / go code>。 我正在开发一个名为
myproj code>的项目,这是一个名为
myproj code>的软件包。 p>
$ GOPATH / src / myproj / myproj.go
$ GOPATH / src / myproj /myproj_test.go
...
code> pre>
go.example / hello / hello.go
go.example / LICENSE
code> pre>
go。 示例 code>是存储库的名称。 p>
$ GOPATH / SRC / go.example /你好/ hello.go
$的GOPATH / SRC / go.example /许可
代码> PRE>
import“ go.example / hello” code>,而不是
import“ hello” code>。 p>
$ GOPATH / src / myproj#git repo的目录
$ GOPATH / src / myproj / .git
$ GOPATH / src / myproj / LICENSE#存储库中的文件
$ GOPATH / src / myproj / myproj / myproj.go#打包dir目录中的文件
$ GOPATH / src / myproj / myproj / myproj_test.go
pre>
myproj code>目录绑定git存储库,而我需要内部目录作为package目录。 结果是我需要输入
import“ myproj / myproj” code>而不是
import“ myproj” code>。 p>
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.
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.
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.