英文:
Correct directory structure for a Go Project?
问题
我对Go相对较新,最近创建了一个要上传到GitHub的项目。我尝试按照指南进行操作,但有一个紧迫的问题,为什么我的二进制文件最终会出现在src/
目录下?
我的项目结构如下:
ssm/ - 项目名称
LICENSE
README.md
src/ - 源代码
files.go - 所有的源代码都在这里。
src - 编译后的二进制文件会出现在这里
bin/ - 二进制文件
我将$GOPATH
设置为~/Documents/Programming/Go/
。从我的gopath中,我无法输入go build ssm
,因为它会显示cannot find package
。如果我进入该目录,它会显示can't load package: package .: no Go source files
。
我必须进入src目录并在那里进行编译,这意味着二进制文件不在bin/
目录中。
我做错了什么?
英文:
I'm relatively new to Go and I've recently created a project that's going up on GitHub. I've tried to follow guides but there's a pressing question of why my binaries end up in src/
?
My layout is like this:
ssm/ - Name of project
LICENSE
README.md
src/ - Source Code
files.go - All my source code is here.
src - The compiled binary ends up here
bin/ - Binaries
I set my $GOPATH
to ~/Documents/Programming/Go/
. From my gopath, I can't type go build ssm
because it cannot find package
. If I cd into the directory, it complains it can't load package: package .: no Go source files
.
I have to actually go into src and compile there, which means the binary isn't in bin/
.
What am I doing wrong?
答案1
得分: 8
请参考 https://code.google.com/p/go-wiki/wiki/GithubCodeLayout
为了与 go get
兼容,你的项目包名需要在 github.com 域名下完全限定:
$GOPATH/
src/github.com/<user>/ssm/
.git
LICENSE
README.md
files.go
bin/
请注意,git 仓库的根目录(.git)与 $GOPATH
不同。
另外,go build <package>
将会在当前目录输出一个编译后的可执行文件。如果你想将可执行文件放到 bin/
目录下,请使用 go install <package>
。
英文:
See https://code.google.com/p/go-wiki/wiki/GithubCodeLayout
To be compatible with go get
, your project's package name needs to be fully-qualified under the github.com domain:
$GOPATH/
src/github.com/<user>/ssm/
.git
LICENSE
README.md
files.go
bin/
Note that the base of the git repository (.git) is not the same as the $GOPATH
.
Also, go build <package>
will output a compiled executable to the current directory. If you want the exe to go to bin/
, use go install <package>
instead.
答案2
得分: 2
你的Go代码可以保存在一个工作区中。工作区包含许多源文件(git、hg、svm等)。Go工具了解这种布局。我们这里不需要Makefile或build.xml。基本的目录布局就是一切。如果在任何情况下你要改变文件布局,相应地你需要改变构建。
这是你可以遵循的一般结构:
$GOPATH/
src/
github.com/username/repo/
mypkg/
mysrc1.go
mysrc2.go
cmd/mycmd/
main.go
bin/
mycmd
而且,这是标准的工作区:
$GOPATH/
bin/fixhub # 安装的二进制文件
pkg/darwin_amd64/ # 编译的归档文件
code.google.com/p/goauth2/oauth.a
github.com/...
src/ # 源代码仓库
code.google.com/p/goauth2/
.hg
oauth # 被go-github包使用
...
github.com/
golang/lint/... # 被fixhub包使用
.git
google/go-github/... # 被fixhub包使用
.git
dsymonds/fixhub/
.git
client.go
cmd/fixhub/fixhub.go # 主包
go get会获取许多代码仓库,而go install会将它们构建成二进制文件。这样做方便快捷,可以快速进行Go开发。而且,Go社区中的每个人都遵循相同的方式。这将src、bin和pkg放在主目录中。而且,在创建工作区之前,$HOME/bin已经在我们的路径中。
英文:
Your go code you can kept in a workspace. A workspace contains many source files (git, hg, svm, etc.). The Go tool understand the layout. We don't require Makefile or build.xml here. The basic directory layout is everything. If in any case if you are going to change the file layout, accordingly you need to change the build.
This is the general structure you can follow,
$GOPATH/
src/
github.com/username/repo/
mypkg/
mysrc1.go
mysrc2.go
cmd/mycmd/
main.go
bin/
mycmd
And, this is the standard workspace
$GOPATH/
bin/fixhub # installed binary
pkg/darwin_amd64/ # compiled archives
code.google.com/p/goauth2/oauth.a
github.com/...
src/ # source repositories
code.google.com/p/goauth2/
.hg
oauth # used by package go-github
...
github.com/
golang/lint/... # used by package fixhub
.git
google/go-github/... # used by package fixhub
.git
dsymonds/fixhub/
.git
client.go
cmd/fixhub/fixhub.go # package main
go get fetch many repositories whereas go install builds a binary out of them. It's convenient and easy to go for go development quickly. And, everyone in the go community follows the same. This puts src, bin and pkg into the home directory. And, $HOME/bin is already in our path before creating our workspace.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论